
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>nio · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="advance.html" />
    
    
    <link rel="prev" href="collections.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">Java</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="object-mgmr.html">
            
                <a href="object-mgmr.html">
            
                    
                    对象管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="object-common.html">
            
                <a href="object-common.html">
            
                    
                    对象通用行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="concurrent.html">
            
                <a href="concurrent.html">
            
                    
                    线程和并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="concurrent/how-to.html">
            
                <a href="concurrent/how-to.html">
            
                    
                    线程和线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="concurrent/block-close.html">
            
                <a href="concurrent/block-close.html">
            
                    
                    线程阻塞和关闭
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="concurrent/communicate.html">
            
                <a href="concurrent/communicate.html">
            
                    
                    线程协作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="concurrent/volatile.html">
            
                <a href="concurrent/volatile.html">
            
                    
                    活性失败-volatile
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="concurrent/lock.html">
            
                <a href="concurrent/lock.html">
            
                    
                    安全性失败-锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="concurrent/thread-local.html">
            
                <a href="concurrent/thread-local.html">
            
                    
                    线程局部存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="concurrent/others.html">
            
                <a href="concurrent/others.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="nio.html">
            
                <a href="nio.html">
            
                    
                    nio
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="advance.html">
            
                <a href="advance.html">
            
                    
                    泛型，注解和反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="mem-gc.html">
            
                <a href="mem-gc.html">
            
                    
                    内存模型和GC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="classloader.html">
            
                <a href="classloader.html">
            
                    
                    ClassLoader
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Web</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="spring.html">
            
                <a href="spring.html">
            
                    
                    spring
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">GUI</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="gui-1.html">
            
                <a href="gui-1.html">
            
                    
                    没事写个小界面什么的
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >nio</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="nio">nio</h1>
<ul>
<li>Java&#x7684;IO&#x4F53;&#x7CFB;&#xFF1A;<ul>
<li>&#x65E7;IO</li>
<li>&#x65B0;IO&#xFF1A;nio&#xFF0C;&#x7528;ByteBuffer&#x548C;FileChannel&#x8BFB;&#x5199;</li>
<li>nio&#x901A;&#x9053;&#x7BA1;&#x7406;&#xFF1A;Selector</li>
<li>Okio&#xFF1A;io&#x7684;&#x5C01;&#x88C5;&#xFF0C;&#x597D;&#x50CF;&#x4E0D;&#x5173;nio&#x7684;&#x4E8B;</li>
<li>Netty&#xFF1A;&#x76EE;&#x7684;&#x662F;&#x5FEB;&#x901F;&#x7684;&#x5B9E;&#x73B0;&#x4EFB;&#x4F55;&#x534F;&#x8BAE;&#x7684;server&#x548C;client&#x7AEF;<ul>
<li>&#x6240;&#x4EE5;&#x8BF4;&#x4F60;&#x53EF;&#x4EE5;&#x7528;netty&#x901A;&#x8FC7;channel&#x7B49;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;httpclient&#xFF0C;&#x548C;URLConnection&#x5E73;&#x7EA7;</li>
<li>&#x8FD9;&#x4E2A;&#x8BFE;&#x9898;&#x592A;&#x5927;&#x4E86;&#xFF0C;&#x5E94;&#x8BE5;&#x5206;&#x5C42;&#x6B21;&#x5B66;&#xFF1A;<ul>
<li>&#x7B2C;&#x4E00;&#x5C42;&#x662F;&#x5B98;&#x65B9;&#x7684;&#x6587;&#x6863;&#xFF0C;&#x5199;&#x51E0;&#x4E2A;helloworld</li>
<li>&#x7B2C;&#x4E8C;&#x5C42;&#x5C31;&#x662F;&#x5B98;&#x65B9;&#x7684;example&#xFF0C;&#x7814;&#x7A76;server&#x548C;client</li>
<li>&#x7B2C;&#x4E09;&#x5C42;&#x662F;&#x6743;&#x5A01;&#x6307;&#x5357;&#xFF0C;&#x7814;&#x7A76;TCP&#xFF0C;UDP&#x7B49;&#x7684;&#x5E38;&#x89C1;&#x95EE;&#x9898;&#xFF0C;&#x8C37;&#x6B4C;&#x7684;protobuf&#xFF0C;&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;http&#x670D;&#x52A1;&#x5668;&#x7B49;</li>
</ul>
</li>
<li>github&#xFF1A;<a href="https://github.com/netty/netty" target="_blank">https://github.com/netty/netty</a></li>
</ul>
</li>
<li>&#x5176;&#x4ED6;&#xFF1A;<ul>
<li>Gzip</li>
<li>&#x5927;&#x6587;&#x4EF6;&#x8BFB;&#x5199;&#xFF0C;&#x5982;2G</li>
<li>&#x6587;&#x4EF6;&#x9501;</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>&#x4E3B;&#x9898;&#xFF1A;<ul>
<li>&#x901A;&#x9053;&#x548C;&#x7F13;&#x51B2;&#x5668;&#xFF1A;&#x63D0;&#x9AD8;&#x8BFB;&#x5199;&#x901F;&#x5EA6;&#xFF0C;Channel&#xFF0C;ByteBuffer&#xFF0C;&#x901F;&#x5EA6;&#x600E;&#x4E48;&#x63D0;&#x9AD8;&#x7684;<ul>
<li>ByteBuffer&#x7684;&#x64CD;&#x4F5C;&#x662F;&#x5F88;&#x5E95;&#x5C42;&#x7684;&#xFF0C;&#x5E95;&#x5C42;&#x5C31;&#x5FEB;&#xFF0C;&#x5E95;&#x5C42;&#x600E;&#x4E48;&#x5C31;&#x5FEB;</li>
<li>ByteBuffer&#x503E;&#x5411;&#x4E8E;&#x5927;&#x5757;&#x7684;&#x64CD;&#x4F5C;&#x5B57;&#x8282;&#x6D41;&#xFF0C;&#x5927;&#x5757;&#x5C31;&#x5FEB;</li>
</ul>
</li>
<li>&#x5F02;&#x6B65;IO&#xFF1A;&#x63D0;&#x9AD8;&#x7EBF;&#x7A0B;&#x7684;&#x5229;&#x7528;&#x7387;&#xFF0C;&#x589E;&#x52A0;&#x7CFB;&#x7EDF;&#x541E;&#x5410;&#x91CF;&#xFF0C;selector&#xFF0C;key&#x7B49;&#xFF0C;&#x4F46;&#x4EE5;&#x727A;&#x7272;&#x5B9E;&#x65F6;&#x6027;&#x4E3A;&#x4EE3;&#x4EF7;&#xFF08;&#x6298;&#x8877;&#x662F;&#x6C38;&#x6052;&#x4E0D;&#x53D8;&#x7684;&#x4E3B;&#x9898;&#xFF09;<ul>
<li>channel&#x7BA1;&#x7406;&#xFF1A;&#x5411;Selector&#x6CE8;&#x518C;Channel&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;&#x5B83;&#x7684;select()&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4F1A;&#x4E00;&#x76F4;&#x963B;&#x585E;&#x5230;&#x67D0;&#x4E2A;&#x6CE8;&#x518C;&#x7684;&#x901A;&#x9053;&#x6709;&#x4E8B;&#x4EF6;&#x5C31;&#x7EEA;<ul>
<li>Selector&#x5141;&#x8BB8;&#x5355;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x591A;&#x4E2A; Channel&#x3002;&#x5982;&#x679C;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x6253;&#x5F00;&#x4E86;&#x591A;&#x4E2A;&#x8FDE;&#x63A5;&#xFF08;&#x901A;&#x9053;&#xFF09;&#xFF0C;&#x4F46;&#x6BCF;&#x4E2A;&#x8FDE;&#x63A5;&#x7684;&#x6D41;&#x91CF;&#x90FD;&#x5F88;&#x4F4E;&#xFF0C;&#x4F7F;&#x7528;Selector&#x5C31;&#x4F1A;&#x5F88;&#x65B9;&#x4FBF;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x4E00;&#x4E2A;&#x804A;&#x5929;&#x670D;&#x52A1;&#x5668;&#x4E2D;</li>
</ul>
</li>
<li>&#x600E;&#x4E48;&#x5C31;&#x727A;&#x7272;&#x5B9E;&#x65F6;&#x6027;&#x4E86;&#xFF0C;&#x4E00;&#x7EC4;IO&#xFF0C;&#x8F6E;&#x8BE2;&#x770B;&#x6709;&#x6CA1;&#x6709;&#x53EF;&#x8BFB;&#x4FE1;&#x606F;&#xFF0C;&#x6240;&#x4EE5;&#x4E00;&#x4E2A;IO&#x6765;&#x6D88;&#x606F;&#x4E86;&#xFF0C;&#x4E0D;&#x4F1A;&#x7ACB;&#x523B;&#x5C31;&#x8F6E;&#x8BE2;&#x5230;&#x5B83;</li>
<li>&#x6240;&#x4EE5;&#x8D1F;&#x8D23;&#x8F6E;&#x8BE2;IO&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x8BFB;&#x5230;&#x6D88;&#x606F;&#x5C31;&#x5F97;&#x7ACB;&#x523B;&#x5206;&#x53D1;&#x51FA;&#x53BB;&#xFF0C;&#x5C3D;&#x91CF;&#x4E0D;&#x80FD;&#x6709;&#x8017;&#x65F6;&#x64CD;&#x4F5C;</li>
<li>&#x7279;&#x522B;&#x6CE8;&#x610F;&#xFF1A;<ul>
<li>Channel&#x548C;Selector&#x914D;&#x5408;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;channel.configureBlocking(false)&#x5207;&#x6362;&#x5230;&#x975E;&#x963B;&#x585E;&#x6A21;&#x5F0F;</li>
<li>&#x800C;FileChannel&#x6CA1;&#x6709;&#x975E;&#x963B;&#x585E;&#x6A21;&#x5F0F;&#xFF0C;&#x53EA;&#x6709;Socket&#x76F8;&#x5173;&#x7684;Channel&#x624D;&#x6709;</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="1-&#x901A;&#x9053;&#x548C;&#x7F13;&#x51B2;&#x5668;">1 &#x901A;&#x9053;&#x548C;&#x7F13;&#x51B2;&#x5668;</h2>
<h3 id="11-&#x7B80;&#x4ECB;">1.1 &#x7B80;&#x4ECB;</h3>
<p>java.nio.*&#x5305;&#x7684;&#x5F15;&#x5165;&#x662F;&#x4E3A;&#x4E86;&#x63D0;&#x9AD8;&#x901F;&#x5EA6;&#xFF0C;&#x5E76;&#x4E14;&#x65E7;&#x7684;IO&#x5305;&#x5DF2;&#x7ECF;&#x7528;nio&#x91CD;&#x65B0;&#x5B9E;&#x73B0;&#x8FC7;&#xFF0C;&#x6240;&#x4EE5;&#x5373;&#x4F7F;&#x4F60;&#x4E0D;&#x7528;nio&#xFF0C;&#x4E5F;&#x5DF2;&#x7ECF;&#x6536;&#x76CA;&#x4E86;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x683C;&#x5F0F;&#x53EF;&#x80FD;&#x6BD4;&#x8F83;&#x4E71;&#xFF0C;&#x9700;&#x8981;&#x914D;&#x5408;GetChannel&#x4F8B;&#x5B50;&#x6765;&#x7406;&#x89E3;</p>
<ul>
<li>&#x5982;&#x4F55;&#x7406;&#x89E3;&#xFF1A;<ul>
<li>&#x4F60;&#x8981;&#x8BFB;&#x6570;&#x636E;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x53EF;&#x80FD;&#x8BF4;&#x7684;&#x662F;IO&#x6D41;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x505A;&#x4E00;&#x4E2A;&#x7164;&#x77FF;&#xFF0C;&#x7164;&#x5C31;&#x662F;&#x6570;&#x636E;</li>
<li>&#x901A;&#x9053;&#xFF1A;Channel&#xFF0C;&#x5305;&#x542B;&#x7164;&#x5C42;&#x7684;&#x77FF;&#x85CF;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7164;&#x77FF;&#x91CC;&#x6709;&#x7164;&#x7684;&#x5730;&#x65B9;</li>
<li>&#x7F13;&#x51B2;&#x5668;&#xFF1A;ByteBuffer&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x505A;&#x8FD0;&#x7164;&#x7684;&#x8F66;&#xFF0C;&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x88C5;&#x8F66;&#x548C;&#x5378;&#x8F66;&#x4E5F;&#x662F;&#x6709;&#x610F;&#x4E49;&#x7684;&#x52A8;&#x4F5C;</li>
<li>&#x6211;&#x4EEC;&#x80FD;&#x505A;&#x7684;&#x5C31;&#x662F;&#x6D3E;&#x8FD0;&#x7164;&#x7684;&#x8F66;&#x53BB;&#x901A;&#x9053;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5C06;&#x7F13;&#x51B2;&#x5668;ByteBuffer&#x548C;Channel&#x8FDE;&#x8D77;&#x6765;&#xFF0C;&#x5F80;&#x91CC;&#x9001;&#x7164;&#xFF08;&#x5199;&#xFF09;&#xFF0C;&#x5F80;&#x5916;&#x8FD0;&#x7164;&#xFF08;&#x8BFB;&#xFF09;</li>
<li>-------&#x7F13;&#x4E00;&#x7F13;-------</li>
<li>ByteBuffer&#x662F;&#x5F88;&#x5E95;&#x5C42;&#x7684;&#x7C7B;&#xFF0C;&#x76F4;&#x63A5;&#x5B58;&#x50A8;&#x672A;&#x52A0;&#x5DE5;&#x7684;&#x5B57;&#x8282;<ul>
<li>&#x521D;&#x59CB;&#x5316;&#xFF1A;<ul>
<li>&#x8981;&#x5199;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x5DF2;&#x7ECF;&#x6709;&#x6570;&#x636E;&#x4E86;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x5F97;&#x5230;byte[]</li>
<li>ByteBuffer.wrap(byte[])   //&#x76F8;&#x5F53;&#x4E8E;wrap(array, 0, array.length);</li>
<li>ByteBuffer.wrap(byte[], offset, length) //offset + length&#x4E0D;&#x80FD;&#x8D85;&#x51FA;byte&#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;</li>
<li>&#x8981;&#x8BFB;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x6700;&#x591A;&#x53EA;&#x80FD;&#x62FF;&#x5230;ByteBuffer&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5927;&#x5C0F;</li>
<li>ByteBuffer buff = ByteBuffer.allocate((int) fc.size());</li>
</ul>
</li>
<li>&#x63A5;&#x53E3;&#xFF1A;byte&#x7684;&#x8BFB;&#x5199;&#xFF0C;&#x4E0D;&#x652F;&#x6301;&#x5BF9;&#x8C61;&#xFF0C;&#x8FDE;String&#x4E5F;&#x4E0D;&#x652F;&#x6301;</li>
<li>&#x5C06;&#x6570;&#x636E;&#x653E;&#x5230;ByteBuffer&#x91CC;&#xFF1A;&#x88C5;&#x8F66;<ul>
<li>&#x4E0A;&#x9762;&#x7684;wrap&#x65B9;&#x6CD5;</li>
<li>&#x4E00;&#x7CFB;&#x5217;put&#x65B9;&#x6CD5;&#xFF0C;&#x53EA;&#x652F;&#x6301;&#x57FA;&#x672C;&#x7C7B;&#x578B;</li>
</ul>
</li>
<li>&#x5C06;&#x6570;&#x636E;&#x4ECE;ByteBuffer&#x4E2D;&#x8F6C;&#x51FA;&#x6765;&#xFF1A;&#x5378;&#x8F66;<ul>
<li>&#x4E00;&#x7CFB;&#x5217;get&#x65B9;&#x6CD5;&#xFF0C;&#x53EA;&#x652F;&#x6301;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#xFF0C;&#x6CE8;&#x610F;flip</li>
<li>String str = new String(buff.array(), &quot;utf-8&quot;)&#xFF0C;buff.array()&#xFF0C;&#x8DDF;ByteBuffer&#x6307;&#x9488;&#x65E0;&#x5173;</li>
</ul>
</li>
<li>ByteBuffer&#x5185;&#x90E8;&#x6307;&#x9488;&#xFF1A;<ul>
<li>ByteBuffer&#x91CC;&#x6709;&#x4E2A;&#x6307;&#x9488;</li>
<li>fc.read(buff)&#x4F1A;&#x4ECE;&#x5F80;ByteBuffer&#x91CC;&#x5199;&#xFF08;&#x88C5;&#x8F66;&#xFF09;&#xFF0C;&#x4ECE;&#x54EA;&#x513F;&#x5199;&#xFF0C;&#x603B;&#x6709;&#x4E2A;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#xFF0C;&#x5C31;&#x662F;ByteBuffer&#x6307;&#x9488;&#x7684;&#x4F4D;&#x7F6E;</li>
<li>&#x5199;&#x5B8C;&#xFF0C;&#x6307;&#x9488;&#x76F4;&#x5230;&#x6700;&#x540E;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x7A7A;&#x767D;&#x53EF;&#x5199;&#x533A;&#x57DF;</li>
<li>&#x8BFB;&#x53D6;&#x91CC;&#x9762;&#x7684;&#x4FE1;&#x606F;&#xFF08;&#x5378;&#x8F66;&#xFF09;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x56DE;&#x5230;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;<ul>
<li>flip&#x4E00;&#x4E0B;</li>
</ul>
</li>
<li>positon&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x8DF3;&#x5230;&#x4EFB;&#x610F;&#x4F4D;&#x7F6E;</li>
</ul>
</li>
</ul>
</li>
<li>FileChannel&#xFF1A;FileInputStream, FileOutputStream, RandomAccessFile&#x8FD9;&#x4E09;&#x4E2A;&#x65E7;&#x7C7B;&#x88AB;&#x4FEE;&#x6539;&#x4E86;&#xFF0C;&#x4EE5;&#x652F;&#x6301;channel<ul>
<li>Reader&#x548C;Writer&#x4E0D;&#x76F4;&#x63A5;&#x652F;&#x6301;Channel&#xFF0C;&#x4F46;Channel&#x91CC;&#x63D0;&#x4F9B;&#x4E86;&#x4FBF;&#x5229;&#x65B9;&#x6CD5;&#x6765;&#x652F;&#x6301;&#x4ED6;&#x4EEC;</li>
<li>&#x83B7;&#x5F97;FileChannel&#xFF1A;<ul>
<li>FileChannel fc = new FileOutputStream(&quot;data.txt&quot;).getChannel();  //&#x5199;</li>
<li>FileChannel fc = new FileInputStream(&quot;cl1024.json&quot;).getChannel(); //&#x8BFB;</li>
<li>FileChannel fc = new RandomAccessFile(&quot;data.txt&quot;, &quot;rw&quot;).getChannel(); //&#x53EF;&#x8BFB;&#x53EF;&#x5199;</li>
</ul>
</li>
<li>&#x79FB;&#x52A8;&#x6587;&#x4EF6;&#x6307;&#x9488;&#xFF1A;append&#x5199;&#x65F6;&#xFF0C;&#x65AD;&#x70B9;&#x7EED;&#x4F20;&#x65F6;&#x80FD;&#x7528;<ul>
<li>fc.position(fc.size()); // Move to the end</li>
</ul>
</li>
<li>&#x5199;&#xFF0C;&#x5C06;&#x4E00;&#x4E2A;ByteBuffer&#x5199;&#x5230;Channel&#x91CC;&#xFF1A;<ul>
<li>fc.write(ByteBuffer.wrap(&quot;Some text &quot;.getBytes()));</li>
</ul>
</li>
<li>&#x8BFB;&#xFF0C;&#x5C06;&#x4E00;&#x4E2A;channel&#x91CC;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x8BFB;&#x5230;ByteBuffer&#x91CC;&#xFF0C;&#x8BFB;&#x591A;&#x5C11;&#xFF0C;&#x7531;ByteBuffer&#x7684;&#x957F;&#x5EA6;&#x51B3;&#x5B9A;<ul>
<li>fc.read(buff);</li>
<li>buff.flip(); &#x8BFB;&#x51FA;&#x6765;&#x7684;ByteBuffer&#x4E00;&#x822C;&#x9700;&#x8981;&#x518D;&#x6B21;&#x89E3;&#x6790;&#x51FA;&#x6765;&#xFF0C;&#x901A;&#x8FC7;getInt,getFloat&#x7B49;&#x64CD;&#x4F5C;&#xFF0C;&#x8BFB;&#x5199;&#x5207;&#x6362;&#x65F6;&#xFF0C;&#x9700;&#x8981;flip&#x4E00;&#x4E0B;</li>
<li>flip&#x600E;&#x4E48;&#x7406;&#x89E3;&#xFF1A;fc.read(buff)&#xFF0C;ByteBuffer&#x91CC;&#x6709;&#x4E2A;&#x6307;&#x9488;<ul>
<li>fc.read(buff)&#x4F1A;&#x4ECE;&#x5F80;ByteBuffer&#x91CC;&#x5199;&#xFF0C;&#x4ECE;&#x54EA;&#x513F;&#x5199;&#xFF0C;&#x603B;&#x6709;&#x4E2A;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#xFF0C;&#x5C31;&#x662F;ByteBuffer&#x6307;&#x9488;&#x7684;&#x4F4D;&#x7F6E;</li>
<li>&#x5199;&#x5B8C;&#xFF0C;&#x6307;&#x9488;&#x76F4;&#x5230;&#x6700;&#x540E;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x7A7A;&#x767D;&#x53EF;&#x5199;&#x533A;&#x57DF;</li>
<li>&#x6240;&#x4EE5;&#x73B0;&#x5728;&#x5C31;&#x597D;&#x7406;&#x89E3;&#x4E86;&#xFF0C;&#x8BFB;&#x5B8C;&#x6587;&#x4EF6;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5F80;ByteBuffer&#x5199;&#x5B8C;&#xFF0C;&#x6307;&#x9488;&#x6307;&#x5411;ByteBuffer&#x6700;&#x540E;&#xFF0C;&#x4F60;&#x518D;&#x8BFB;&#x53D6;&#x91CC;&#x9762;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x56DE;&#x5230;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>&#x603B;&#x7ED3;&#xFF1A;<ul>
<li>FileInputStream&#xFF0C;FileOutputStream&#xFF0C;&#x8FD9;&#x76F8;&#x5F53;&#x4E8E;&#x7164;&#x77FF;<ul>
<li>&#x4EE5;&#x524D;&#x4F60;&#x76F4;&#x63A5;&#x64CD;&#x4F5C;stream&#x7684;read&#xFF0C;write&#xFF0C;&#x53C2;&#x6570;&#x662F;byte[]</li>
<li>read&#xFF0C;write&#x76F4;&#x63A5;&#x64CD;&#x4F5C;&#x7164;&#x77FF;</li>
<li>&#x76F4;&#x63A5;&#x901A;&#x8FC7;byte[]&#x8BFB;&#x5199;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x7528;&#x94C1;&#x9528;&#x94F2;&#x7164;</li>
</ul>
</li>
<li>&#x5728;new io&#x91CC;&#xFF0C;&#x4F60;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x64CD;&#x4F5C;&#x7164;&#x77FF;&#x4E86;&#xFF0C;&#x800C;&#x662F;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#xFF1A;FileChannel<ul>
<li>&#x901A;&#x8FC7;channel&#x7684;read&#xFF0C;write&#x6765;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#xFF0C;position&#xFF0C;seek&#x7B49;&#xFF0C;&#x5C31;&#x662F;&#x79FB;&#x52A8;&#x6307;&#x9488;&#xFF08;&#x6587;&#x4EF6;&#x6307;&#x9488;&#xFF09;</li>
<li>read&#xFF0C;write&#x7684;&#x53C2;&#x6570;&#x662F;ByteBuffer</li>
<li>&#x901A;&#x8FC7;ByteBuffer&#x6765;&#x5305;&#x88C5;&#x6570;&#x636E;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x7528;&#x8F66;&#x62C9;&#x7164;</li>
</ul>
</li>
<li>&#x7531;&#x4E8E;&#x628A;byte[]&#x7528;ByteBuffer&#x5305;&#x88C5;&#x8D77;&#x6765;&#xFF0C;&#x53C8;&#x9762;&#x4E34;&#x4E00;&#x4E2A;&#x88C5;&#x8F66;&#x548C;&#x5378;&#x8F66;&#x7684;&#x95EE;&#x9898;<ul>
<li>&#x88C5;&#x8F66;&#xFF1A;&#x5199;&#x6587;&#x4EF6;&#xFF08;wrap, put&#x7B49;&#x65B9;&#x6CD5;&#xFF09;&#xFF0C;&#x8BFB;&#x6587;&#x4EF6;&#xFF08;channel.read(buff)&#xFF09;</li>
<li>&#x5378;&#x8F66;&#xFF1A;&#x8BFB;&#x6587;&#x4EF6;&#xFF08;get&#x5404;&#x79CD;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#xFF09;&#xFF0C;&#x5199;&#x6587;&#x4EF6;&#xFF08;channel.write(buff)&#xFF09;</li>
<li>&#x5168;&#x8F66;&#x64CD;&#x4F5C;&#xFF1A;array</li>
<li>&#x6CE8;&#x610F;flip&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x8BFB;&#x5199;&#x5207;&#x6362;&#x65F6;&#xFF0C;&#x9700;&#x8981;flip&#x4E00;&#x4E0B;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x8FD8;&#x4E0D;&#x786E;&#x5B9A;&#x5C31;&#x662F;&#x6307;&#x9488;&#x64CD;&#x4F5C;</li>
<li>&#x6CE8;&#x610F;rewind&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x8BFB;&#x7740;&#x8BFB;&#x7740;&#xFF0C;&#x60F3;&#x56DE;&#x5934;&#x4ECE;&#x5934;&#x518D;&#x8BFB;&#xFF0C;&#x5C31;&#x5F97;rewind&#xFF0C;&#x8FD9;&#x4E2A;&#x80AF;&#x5B9A;&#x662F;&#x6307;&#x9488;&#x64CD;&#x4F5C;</li>
<li>buff.hasRemaining()&#xFF0C;&#x6307;&#x9488;&#x662F;&#x5426;&#x5230;&#x5934;&#x4E86;</li>
</ul>
</li>
<li>&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;Channel&#x548C;ByteBuffer&#x63D0;&#x4F9B;&#x7684;&#x63A5;&#x53E3;&#x90FD;&#x6BD4;&#x8F83;&#x4F4E;&#x7EA7;&#xFF0C;&#x76F4;&#x63A5;&#x548C;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x5951;&#x5408;&#xFF0C;&#x8BF4;&#x662F;&#x8FD9;&#x5C31;&#x662F;&#x5FEB;&#x7684;&#x539F;&#x56E0;</li>
</ul>
</li>
</ul>
<ul>
<li>&#x5173;&#x4E8E;Channel&#xFF1A;<ul>
<li>FileChannel</li>
<li>DatagramChannel&#xFF1A;&#x901A;&#x8FC7;UDP&#x8BFB;&#x5199;&#x7F51;&#x7EDC;&#xFF0C;&#x65E0;&#x8FDE;&#x63A5;&#x7684;</li>
<li>SocketChannel&#xFF1A;&#x901A;&#x8FC7;TCP&#x8BFB;&#x5199;&#x7F51;&#x7EDC;</li>
<li>ServerSocketChannel&#xFF1A;&#x76D1;&#x542C;&#x65B0;&#x6765;&#x7684;TCP&#x8FDE;&#x63A5;&#xFF0C;&#x6BCF;&#x4E2A;&#x65B0;&#x8FDB;&#x6765;&#x7684;&#x8FDE;&#x63A5;&#x90FD;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;SocketChannel</li>
</ul>
</li>
</ul>
<p>&#x4F8B;&#x5B50;&#xFF0C;&#x4EE3;&#x7801;&#x6BD4;&#x8F83;&#x77ED;&#xFF0C;&#x76F4;&#x63A5;&#x8D34;&#x8FC7;&#x6765;</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.cowthan.nio;

<span class="hljs-keyword">import</span> java.nio.*;
<span class="hljs-keyword">import</span> java.nio.channels.*;
<span class="hljs-keyword">import</span> java.io.*;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GetChannel</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> BSIZE = <span class="hljs-number">1024</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception </span>{

        <span class="hljs-comment">// &#x5199;&#x6587;&#x4EF6;</span>
        FileChannel fc = <span class="hljs-keyword">new</span> FileOutputStream(<span class="hljs-string">&quot;data.txt&quot;</span>).getChannel();
        fc.write(ByteBuffer.wrap(<span class="hljs-string">&quot;Some text &quot;</span>.getBytes()));  <span class="hljs-comment">//</span>
        fc.close();

        <span class="hljs-comment">// &#x5199;&#x6587;&#x4EF6;&#xFF1A;append</span>
        fc = <span class="hljs-keyword">new</span> RandomAccessFile(<span class="hljs-string">&quot;data.txt&quot;</span>, <span class="hljs-string">&quot;rw&quot;</span>).getChannel();
        fc.position(fc.size()); <span class="hljs-comment">// Move to the end</span>
        fc.write(ByteBuffer.wrap(<span class="hljs-string">&quot;Some more&quot;</span>.getBytes()));
        fc.close();

        <span class="hljs-comment">// &#x8BFB;&#x6587;&#x4EF6;</span>
        fc = <span class="hljs-keyword">new</span> FileInputStream(<span class="hljs-string">&quot;data.txt&quot;</span>).getChannel();
        ByteBuffer buff = ByteBuffer.allocate((<span class="hljs-keyword">int</span>) fc.size());
        fc.read(buff);
        buff.flip();


        System.out.println(<span class="hljs-string">&quot;&#x8BFB;&#x53D6;&#xFF1A;&quot;</span>);
        String str = <span class="hljs-keyword">new</span> String(buff.array(), <span class="hljs-string">&quot;utf-8&quot;</span>);
        System.out.println(str);

        System.out.println(<span class="hljs-string">&quot;&#x8BFB;&#x53D6;2&#xFF1A;&quot;</span>);
        <span class="hljs-keyword">while</span> (buff.hasRemaining()){
            System.out.print((<span class="hljs-keyword">char</span>) buff.get());
        }
    }
} <span class="hljs-comment">/*
 * Output: Some text Some more
 */</span><span class="hljs-comment">// :~</span>
</code></pre>
<h3 id="12-&#x66F4;&#x591A;&#xFF1A;flip-clear&#xFF0C;compact&#x548C;mark&#xFF0C;reset&#x64CD;&#x4F5C;">1.2 &#x66F4;&#x591A;&#xFF1A;flip, clear&#xFF0C;compact&#x548C;mark&#xFF0C;reset&#x64CD;&#x4F5C;</h3>
<ul>
<li>flip&#xFF0C;clear&#xFF0C;compact&#x548C;mark&#xFF0C;reset<ul>
<li>&#x8FD9;&#x91CC;&#x8BF4;&#x7684;&#x8BFB;&#x5199;&#x90FD;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;ByteBuffer</li>
<li>&#x7531;&#x5199;&#x8F6C;&#x8BFB;&#xFF1A;flip</li>
<li>&#x7531;&#x5199;&#x8F6C;&#x8BFB;&#xFF1A;clear&#x6E05;&#x7A7A;&#x7F13;&#x51B2;&#x533A;&#xFF0C;compact&#x6E05;&#x7A7A;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x5DF2;&#x8BFB;&#x6570;&#x636E;&#xFF08;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#x518D;&#x88C5;&#x8F66;&#xFF0C;&#x5C31;&#x662F;&#x4ECE;&#x672A;&#x8BFB;&#x6570;&#x636E;&#x540E;&#x9762;&#x5F00;&#x59CB;&#xFF09;</li>
<li>&#x968F;&#x673A;&#x8BFB;&#x5199;&#xFF1A;mark&#x548C;reset&#xFF0C;&#x5982;&#x679C;&#x8981;&#x4E00;&#x4F1A;&#x5199;&#x4E00;&#x4F1A;&#x8BFB;&#xFF0C;mark&#x4F1A;&#x8BB0;&#x5F55;&#x5F53;&#x524D;position&#xFF0C;position&#x5C31;&#x662F;&#x8BFB;&#x5199;&#x7684;&#x8D77;&#x70B9;&#xFF0C;reset&#x4F1A;&#x56DE;&#x6EDA;</li>
<li>ByteBuffer.allocate(len)&#x7684;&#x5927;&#x5C0F;&#x95EE;&#x9898;&#xFF0C;&#x5927;&#x5757;&#x7684;&#x79FB;&#x52A8;&#x6570;&#x636E;&#x662F;&#x5FEB;&#x7684;&#x5173;&#x952E;&#xFF0C;&#x6240;&#x4EE5;&#x957F;&#x5EA6;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x4F46;&#x6CA1;&#x5565;&#x6807;&#x51C6;&#xFF0C;&#x6839;&#x636E;&#x60C5;&#x51B5;&#x5B9A;&#x5427;&#xFF0C;1024&#xFF08;1K&#xFF09;&#x5C0F;&#x4E86;</li>
<li>ByteBuffer.wrap(byte[])&#xFF0C;&#x4E0D;&#x4F1A;&#x518D;&#x590D;&#x5236;&#x6570;&#x7EC4;&#xFF0C;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x4EE5;&#x53C2;&#x6570;&#x4E3A;&#x5E95;&#x5C42;&#x6570;&#x7EC4;&#xFF0C;&#x5FEB;</li>
<li>&#x590D;&#x5236;&#x6587;&#x4EF6;&#x65F6;&#xFF0C;&#x4E00;&#x4E2A;ByteBuffer&#x5BF9;&#x8C61;&#x4F1A;&#x4E0D;&#x65AD;&#x4ECE;src&#x7684;channel&#x6765;read&#xFF0C;&#x5E76;&#x5199;&#x5165;dest&#x7684;channel&#xFF0C;&#x6CE8;&#x610F;&#xFF1A;<ul>
<li>src.read(buff);  buff.flip();  dest.write(buff); buff.clear()</li>
<li>ByteBuffer&#x5FC5;&#x987B;clear&#x4E86;&#xFF0C;&#x624D;&#x80FD;&#x91CD;&#x65B0;&#x4ECE;Channel&#x8BFB;</li>
</ul>
</li>
<li>ByteBuffer.flip(), clear()&#x6BD4;&#x8F83;&#x62D9;&#x52A3;&#xFF0C;&#x4F46;&#x8FD9;&#x6B63;&#x662F;&#x4E3A;&#x4E86;&#x6700;&#x5927;&#x901F;&#x5EA6;&#x4ED8;&#x51FA;&#x7684;&#x4EE3;&#x4EF7;</li>
</ul>
</li>
</ul>
<pre><code class="lang-java"><span class="hljs-comment">///&#x590D;&#x5236;&#x6587;&#x4EF6;&#x7684;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#xFF08;&#x66F4;&#x4F18;&#x5316;&#x7684;&#x590D;&#x5236;&#x6587;&#x4EF6;&#x662F;&#x7528;transfer&#x63A5;&#x53E3;&#xFF0C;&#x76F4;&#x63A5;&#x901A;&#x9053;&#x76F8;&#x8FDE;&#xFF09;</span>
ByteBuffer buff = ByteBuffer.allocate(<span class="hljs-number">1024</span>); <span class="hljs-comment">//1K</span>
<span class="hljs-keyword">while</span>(src.read(buff) != -<span class="hljs-number">1</span>){
    buff.flip(); <span class="hljs-comment">//&#x51C6;&#x5907;&#x5378;&#x8F66;</span>
    dest.write(buff); <span class="hljs-comment">//&#x5378;&#x8F66;&#x4E86;</span>
    buff.clear(); <span class="hljs-comment">//&#x5176;&#x5B9E;&#x8FD9;&#x624D;&#x662F;&#x771F;&#x6B63;&#x7684;&#x5378;&#x8F66;&#xFF0C;&#x5E76;&#x9001;&#x56DE;&#x901A;&#x9053;&#x90A3;&#x5934;&#xFF08;&#x53EF;&#x4EE5;&#x518D;&#x6B21;read(buff)&#x4E86;&#xFF09;</span>
}
</code></pre>
<p><code>&#x7F13;&#x51B2;&#x5668;&#x7EC6;&#x8282;&#xFF1A;&#x56DB;&#x5927;&#x7D22;&#x5F15;</code></p>
<p>&#x770B;&#x56FE;&#xFF1A;<br><img src="https://raw.githubusercontent.com/cowthan/JavaAyo/master/doc/img/buffers-modes.png" alt=""></p>
<ul>
<li>&#x56DB;&#x5927;&#x7D22;&#x5F15;&#xFF1A;<ul>
<li>mark&#xFF1A;&#x6807;&#x8BB0;&#xFF0C;mark&#x65B9;&#x6CD5;&#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x4F4D;&#x7F6E;&#xFF0C;reset&#x65B9;&#x6CD5;&#x56DE;&#x6EDA;&#x5230;&#x4E0A;&#x6B21;mark&#x7684;&#x4F4D;&#x7F6E;</li>
<li>position&#xFF1A;&#x4F4D;&#x7F6E;&#xFF0C;&#x5F53;&#x524D;&#x4F4D;&#x7F6E;&#xFF0C;&#x8BFB;&#x548C;&#x5199;&#x90FD;&#x662F;&#x5728;&#x8FD9;&#x4E2A;&#x4F4D;&#x7F6E;&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;&#x4E14;&#x4F1A;&#x5F71;&#x54CD;&#x8FD9;&#x4E2A;&#x4F4D;&#x7F6E;&#xFF0C;position&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;seek</li>
<li>limit&#xFF1A;&#x754C;&#x9650;&#xFF0C;<ul>
<li>&#x4F5C;&#x4E3A;&#x8BFB;&#x7684;&#x754C;&#x9650;&#x65F6;&#xFF1A;&#x6307;&#x5230;buffer&#x5F53;&#x524D;&#x88AB;&#x586B;&#x5165;&#x4E86;&#x591A;&#x5C11;&#x6570;&#x636E;&#xFF0C;get&#x65B9;&#x6CD5;&#x4EE5;&#x6B64;&#x4E3A;&#x754C;&#x9650;&#xFF0C;<ul>
<li>flip&#x4E00;&#x4E0B;&#xFF0C;limit&#x624D;&#x6709;&#x503C;&#xFF0C;&#x6307;&#x5411;postion&#xFF0C;&#x624D;&#x80FD;&#x6709;&#x4E2A;&#x8BFB;&#x7684;&#x754C;&#x9650;</li>
</ul>
</li>
<li>&#x4F5C;&#x4E3A;&#x5199;&#x7684;&#x754C;&#x9650;&#x65F6;&#xFF1A;<ul>
<li>allocate&#x6216;&#x8005;clear&#x65F6;&#xFF0C;&#x76F4;&#x63A5;&#x53EF;&#x5199;&#xFF0C;limit&#x6307;&#x5411;capacity&#xFF0C;&#x8868;&#x793A;&#x6700;&#x591A;&#x5199;&#x5230;&#x8FD9;</li>
<li>wrap&#x65F6;&#xFF0C;&#x76F4;&#x63A5;&#x53EF;&#x8BFB;&#xFF0C;&#x6240;&#x4EE5;position&#x662F;0&#xFF0C;limit&#x662F;&#x6307;&#x5230;&#x4E4B;&#x540E;&#xFF0C;capacity&#x4E5F;&#x662F;&#x6307;&#x5230;&#x6700;&#x540E;&#xFF0C;&#x76F4;&#x63A5;&#x8FDB;&#x5165;&#x53EF;&#x8BFB;&#x72B6;&#x6001;</li>
</ul>
</li>
</ul>
</li>
<li>capacity&#xFF1A;&#x5BB9;&#x91CF;&#xFF0C;&#x6307;&#x5230;buffer&#x7684;&#x6700;&#x540E;&#xFF0C;&#x8FD9;&#x4E0D;&#x662F;&#x5B57;&#x8282;&#x6570;&#xFF0C;&#x800C;&#x662F;&#x80FD;&#x5199;&#x5165;&#x7684;&#x4E2A;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;ByteBuffer&#xFF0C;&#x5C31;&#x662F;byte&#x4E2A;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;IntBuffer&#xFF0C;&#x5C31;&#x662F;int&#x4E2A;&#x6570;<ul>
<li>allocate&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x5C31;&#x662F;capacity<ul>
<li>&#x6240;&#x4EE5;&#xFF0C;&#x53EF;&#x4EE5;&#x63A8;&#x65AD;&#x4E00;&#x4E0B;&#xFF0C;ByteBuffer.capacity = 5&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x8F6C;&#x6210;IntBuffer&#xFF0C;capacity&#x662F;1&#xFF0C;&#x4E0D;&#x4F1A;&#x6307;&#x5411;&#x6700;&#x540E;&#xFF0C;&#x800C;&#x662F;&#x7559;&#x51FA;&#x4E86;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#xFF0C;&#x88AB;&#x5FFD;&#x7565;&#x4E86;&#xFF0C;&#x6CA1;&#x6CD5;&#x901A;&#x8FC7;Int&#x8BFB;&#x5199;</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&#x5BF9;&#x5E94;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;
<img src="https://raw.githubusercontent.com/cowthan/JavaAyo/master/doc/img/nio2.png" alt=""></p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Buffer <span class="hljs-title">flip</span><span class="hljs-params">()</span> </span>{
    limit = position;
    position = <span class="hljs-number">0</span>;
    mark = UNSET_MARK;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Buffer <span class="hljs-title">rewind</span><span class="hljs-params">()</span> </span>{
    position = <span class="hljs-number">0</span>;
    mark = UNSET_MARK;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasRemaining</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">return</span> position &lt; limit;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Buffer <span class="hljs-title">clear</span><span class="hljs-params">()</span> </span>{
    position = <span class="hljs-number">0</span>;
    mark = UNSET_MARK;
    limit = capacity;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}


<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Buffer <span class="hljs-title">mark</span><span class="hljs-params">()</span> </span>{
    mark = position;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> Buffer <span class="hljs-title">reset</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">if</span> (mark == UNSET_MARK) {
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> InvalidMarkException(<span class="hljs-string">&quot;Mark not set&quot;</span>);
    }
    position = mark;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}
</code></pre>
<p>&#x4F8B;&#x5B50;&#xFF1A;&#x4EA4;&#x6362;&#x76F8;&#x90BB;&#x7684;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x7ED9;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4EA4;&#x6362;&#x76F8;&#x90BB;&#x7684;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">symmetricScramble</span><span class="hljs-params">(CharBuffer buffer)</span> </span>{
    <span class="hljs-keyword">while</span> (buffer.hasRemaining()) {
        buffer.mark();
        <span class="hljs-keyword">char</span> c1 = buffer.get();
        <span class="hljs-keyword">char</span> c2 = buffer.get();
        buffer.reset();
        buffer.put(c2).put(c1);
    }
}

<span class="hljs-comment">/*
&#x601D;&#x8003;&#xFF1A;&#x5982;&#x679C;&#x6CA1;&#x6709;mark&#x548C;reset&#x529F;&#x80FD;&#xFF0C;&#x4F60;&#x600E;&#x4E48;&#x505A;&#xFF1F;&#x7528;postion&#x65B9;&#x6CD5;&#x8BB0;&#x5F55;&#x548C;&#x6062;&#x590D;&#x521A;&#x624D;&#x4F4D;&#x7F6E;
*/</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">symmetricScramble2</span><span class="hljs-params">(CharBuffer buffer)</span> </span>{
    <span class="hljs-keyword">while</span> (buffer.hasRemaining()) {
        <span class="hljs-keyword">int</span> position = buffer.position();
        <span class="hljs-keyword">char</span> c1 = buffer.get();
        <span class="hljs-keyword">char</span> c2 = buffer.get();
        buffer.position(position);
        buffer.put(c2).put(c1);
    }
}
</code></pre>
<ul>
<li>&#x603B;&#x7ED3;&#xFF1A;<ul>
<li>flip&#xFF1A;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x7531;&#x5199;&#x8F6C;&#x8BFB;&#xFF0C;flip&#x4E4B;&#x540E;&#x53EF;&#x4EE5;&#xFF1A;<ul>
<li>&#x8BFB;&#xFF1A;&#x662F;&#x4ECE;&#x5934;&#x8BFB;&#xFF0C;&#x80FD;&#x8BFB;&#x5230;&#x521A;&#x624D;&#x5199;&#x7684;&#x957F;&#x5EA6;</li>
<li>&#x5199;&#xFF1A;&#x662F;&#x4ECE;&#x5934;&#x5199;&#xFF0C;&#x4F1A;&#x8986;&#x76D6;&#x521A;&#x624D;&#x5199;&#x5165;&#x7684;&#x5185;&#x5BB9;</li>
</ul>
</li>
<li>clear&#xFF1A;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x8BFB;&#x8F6C;&#x5199;&#xFF0C;clear&#x4E4B;&#x540E;&#x53EF;&#x4EE5;&#xFF1A;<ul>
<li>&#x8BFB;&#xFF1A;&#x4F46;&#x662F;&#x8BFB;&#x4E0D;&#x5230;&#x4EC0;&#x4E48;&#x4E86;</li>
<li>&#x5199;&#xFF1A;&#x662F;&#x4ECE;&#x5934;&#x5199;</li>
</ul>
</li>
<li>mark&#x548C;reset&#xFF1A;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x8BFB;&#x5199;&#x4EA4;&#x66FF;<ul>
<li>mark&#xFF1A;&#x76F8;&#x5F53;&#x4E8E;int postion = buffer.postion()&#xFF0C;&#x8BB0;&#x4E0B;&#x5F53;&#x524D;&#x4F4D;&#x7F6E;</li>
<li>reset&#xFF1A;&#x76F8;&#x5F53;&#x4E8E;buffer.postion(position)&#xFF0C;&#x56DE;&#x5230;&#x521A;&#x624D;&#x8BB0;&#x5F55;&#x7684;&#x4F4D;&#x7F6E; </li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="13-&#x8FDE;&#x63A5;&#x901A;&#x9053;">1.3 &#x8FDE;&#x63A5;&#x901A;&#x9053;</h3>
<p>&#x4E0A;&#x9762;&#x8BF4;&#x8FC7;&#xFF0C;nio&#x901A;&#x8FC7;&#x5927;&#x5757;&#x6570;&#x636E;&#x7684;&#x79FB;&#x52A8;&#x6765;&#x52A0;&#x5FEB;&#x8BFB;&#x5199;&#x901F;&#x5EA6;&#xFF0C;&#x524D;&#x9762;&#x8FD9;&#x4E2A;&#x5927;&#x5C0F;&#x90FD;&#x7531;ByteBuffer&#x6765;&#x63A7;&#x5236;&#xFF0C;
&#x5176;&#x5B9E;&#x8FD8;&#x6709;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5C06;&#x8BFB;&#x5199;&#x4E24;&#x4E2A;Channel&#x76F8;&#x8FDE;</p>
<p>&#x8FD9;&#x4E5F;&#x662F;&#x5B9E;&#x73B0;&#x6587;&#x4EF6;&#x590D;&#x5236;&#x7684;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TransferTo</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        <span class="hljs-keyword">if</span> (args.length != <span class="hljs-number">2</span>) {
            System.out.println(<span class="hljs-string">&quot;arguments: sourcefile destfile&quot;</span>);
            System.exit(<span class="hljs-number">1</span>);
        }
        FileChannel in = <span class="hljs-keyword">new</span> FileInputStream(args[<span class="hljs-number">0</span>]).getChannel(), out = <span class="hljs-keyword">new</span> FileOutputStream(
                args[<span class="hljs-number">1</span>]).getChannel();
        in.transferTo(<span class="hljs-number">0</span>, in.size(), out);
        <span class="hljs-comment">// &#x6216;&#x8005;&#xFF1A;</span>
        <span class="hljs-comment">// out.transferFrom(in, 0, in.size());</span>
    }
} <span class="hljs-comment">// /:~</span>
</code></pre>
<h3 id="14-&#x5B57;&#x7B26;&#x6D41;&#xFF1A;charbuffer&#x548C;charset&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;byte&#x548C;&#x7F16;&#x7801;&#x95EE;&#x9898;">1.4 &#x5B57;&#x7B26;&#x6D41;&#xFF1A;CharBuffer&#x548C;Charset&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;byte[]&#x548C;&#x7F16;&#x7801;&#x95EE;&#x9898;</h3>
<p>ByteBuffer&#x662F;&#x6700;&#x539F;&#x59CB;&#x7684;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x5B57;&#x8282;&#x6D41;&#xFF0C;&#x9002;&#x7528;&#x4E8E;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x7684;&#x8BFB;&#x5199;&#xFF0C;&#x56FE;&#x7247;&#x6587;&#x4EF6;&#x7B49;</p>
<p>&#x4F46;&#x6211;&#x4EEC;&#x66F4;&#x5E38;&#x7528;&#x7684;&#xFF0C;&#x5176;&#x5B9E;&#x662F;&#x5B57;&#x7B26;&#x4E32;</p>
<ul>
<li><p>&#x5B57;&#x7B26;&#x4E32;&#x6D89;&#x53CA;&#x5230;&#x7684;&#x7C7B;&#xFF1A;</p>
<ul>
<li>CharBuffer&#xFF1A;&#x6CE8;&#x610F;&#xFF0C;Channel&#x662F;&#x76F4;&#x63A5;&#x548C;ByteBuffer&#x4EA4;&#x6D41;&#xFF0C;&#x6240;&#x4EE5;CharBuffer&#x53EA;&#x80FD;&#x7B97;&#x662F;&#x4E0A;&#x5C42;&#x5C01;&#x88C5;</li>
<li>Charset&#xFF1A;&#x7F16;&#x7801;&#x76F8;&#x5173;&#xFF0C;&#x5B57;&#x8282;&#x6D41;&#x5230;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x80AF;&#x5B9A;&#x4F1A;&#x6709;&#x7F16;&#x7801;&#x76F8;&#x5173;&#x7684;&#x95EE;&#x9898;</li>
<li>CharBuffer.toString()&#xFF1A;&#x5F97;&#x5230;&#x5B57;&#x7B26;&#x4E32;</li>
</ul>
</li>
<li><p>&#x600E;&#x4E48;&#x5F97;&#x5230;CharBuffer</p>
<ul>
<li>&#x65B9;&#x6CD5;1&#xFF1A;ByteBuffer.asCharBuffer()&#xFF0C;&#x5C40;&#x9650;&#x5728;&#x4E8E;&#x4F7F;&#x7528;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7F16;&#x7801;</li>
<li>&#x65B9;&#x6CD5;2&#xFF1A;Charset.forName(&quot;utf-8&quot;).decode(buff)&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;new String(buff.array(), &quot;utf-8&quot;)&#x7684;&#x9AD8;&#x7EA7;&#x7248;<ul>
<li>&#x76F8;&#x5BF9;&#x7684;&#xFF0C;Charset.forName(&quot;utf-8&quot;).encode(cbuff)&#xFF0C;&#x8FD4;&#x56DE;&#x4E2A;ByteBuffer&#xFF0C;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;String.getBytes(&quot;utf-8)</li>
</ul>
</li>
</ul>
</li>
<li><p>CharBuffer&#x8BFB;&#x5199;</p>
<ul>
<li>put(String)&#xFF1A;&#x5199;</li>
<li>toString()&#xFF1A;&#x8BFB;&#xFF0C;&#x5C31;&#x62FF;&#x5230;&#x4E86;&#x5B57;&#x7B26;&#x4E32;</li>
</ul>
</li>
</ul>
<p><code>====&#x5148;&#x4F11;&#x606F;&#x4E00;&#x4E0B;&#xFF0C;&#x8BF4;&#x8BF4;&#x600E;&#x4E48;&#x5F97;&#x5230;&#x7F16;&#x7801;&#x76F8;&#x5173;&#x7684;&#x4E00;&#x4E9B;&#x4FE1;&#x606F;&#x5427;====</code></p>
<pre><code class="lang-java"><span class="hljs-comment">//&#x6253;&#x5370;&#x7CFB;&#x7EDF;&#x652F;&#x6301;&#x7684;&#x6240;&#x6709;&#x7F16;&#x7801;&#xFF0C;&#x53CA;&#x5176;&#x522B;&#x540D;</span>
<span class="hljs-keyword">import</span> java.nio.charset.*;
<span class="hljs-keyword">import</span> java.util.*;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AvailableCharSets</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        SortedMap&lt;String, Charset&gt; charSets = Charset.availableCharsets();
        Iterator&lt;String&gt; it = charSets.keySet().iterator();
        <span class="hljs-keyword">while</span> (it.hasNext()) {
            String csName = it.next();
            System.out.print(csName);
            Iterator aliases = charSets.get(csName).aliases().iterator();
            <span class="hljs-keyword">if</span> (aliases.hasNext())
                System.out.print(<span class="hljs-string">&quot;: &quot;</span>);
            <span class="hljs-keyword">while</span> (aliases.hasNext()) {
                System.out.print(aliases.next());
                <span class="hljs-keyword">if</span> (aliases.hasNext())
                    System.out.print(<span class="hljs-string">&quot;, &quot;</span>);
            }
            System.out.println();
        }
    }
}
<span class="hljs-comment">/*
&#x90E8;&#x5206;&#x8F93;&#x51FA;&#xFF1A;
KOI8-U: koi8_u
Shift_JIS: shift_jis, x-sjis, sjis, shift-jis, ms_kanji, csShiftJIS
TIS-620: tis620, tis620.2533
US-ASCII: ANSI_X3.4-1968, cp367, csASCII, iso-ir-6, ASCII, iso_646.irv:1983, ANSI_X3.4-1986, ascii7, default, ISO_646.irv:1991, ISO646-US, IBM367, 646, us
UTF-16: UTF_16, unicode, utf16, UnicodeBig
UTF-16BE: X-UTF-16BE, UTF_16BE, ISO-10646-UCS-2, UnicodeBigUnmarked
UTF-16LE: UnicodeLittleUnmarked, UTF_16LE, X-UTF-16LE
UTF-32: UTF_32, UTF32
UTF-32BE: X-UTF-32BE, UTF_32BE
UTF-32LE: X-UTF-32LE, UTF_32LE
UTF-8: unicode-1-1-utf-8, UTF8
windows-1250: cp1250, cp5346
windows-1251: cp5347, ansi-1251, cp1251
windows-1252: cp5348, cp1252
windows-1253: cp1253, cp5349
*/</span>
</code></pre>
<p><code>=====ByteBuffer.asCharBuffer()&#x7684;&#x5C40;&#x9650;&#xFF1A;&#x6CA1;&#x6307;&#x5B9A;&#x7F16;&#x7801;&#xFF0C;&#x5BB9;&#x6613;&#x4E71;&#x7801;=====</code></p>
<ul>
<li>&#x8FD9;&#x4E2A;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x4E0D;&#x80FD;&#x7528;&#xFF0C;&#x4E3A;&#x4F55;&#xFF1A;<ul>
<li>asCharBuffer()&#x4F1A;&#x628A;ByteBuffer&#x8F6C;&#x4E3A;CharBuffer&#xFF0C;&#x4F46;&#x7528;&#x7684;&#x662F;&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7F16;&#x7801;</li>
</ul>
</li>
</ul>
<h3 id="15-&#x89C6;&#x56FE;&#x7F13;&#x51B2;&#x5668;&#xFF1A;shortbuffer&#xFF0C;intbuffer-longbuffer&#xFF0C;floatbuffer&#xFF0C;doublebuffer&#xFF0C;charbuffer">1.5 &#x89C6;&#x56FE;&#x7F13;&#x51B2;&#x5668;&#xFF1A;ShortBuffer&#xFF0C;IntBuffer, LongBuffer&#xFF0C;FloatBuffer&#xFF0C;DoubleBuffer&#xFF0C;CharBuffer</h3>
<ul>
<li>Buffer&#x7C7B;&#x578B;&#xFF1A;<ul>
<li>ByteBuffer</li>
<li>DoubleBuffer</li>
<li>FloatBuffer</li>
<li>IntBuffer</li>
<li>LongBuffer</li>
<li>ShortBuffer</li>
<li>CharBuffer   &#x5B57;&#x7B26;&#x4E32;&#x7684;&#x7F13;&#x51B2;&#x533A;</li>
<li>MappedByteBuffer  &#x5927;&#x6587;&#x4EF6;&#x7684;&#x7F13;&#x51B2;&#x533A;</li>
</ul>
</li>
</ul>
<p>ByteBuffer&#x7CFB;&#x5217;&#x7684;&#x7C7B;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x633A;&#x6709;&#x610F;&#x601D;&#xFF0C;&#x53EF;&#x4EE5;&#x7814;&#x7A76;&#x7814;&#x7A76;</p>
<p>ByteArrayBuffer&#x662F;&#x5176;&#x6700;&#x901A;&#x7528;&#x5B50;&#x7C7B;&#xFF0C;&#x4E00;&#x822C;&#x64CD;&#x4F5C;&#x7684;&#x90FD;&#x662F;ByteArrayBuffer</p>
<p>ByteBuffer.asLongBuffer(), asIntBuffer(), asDoubleBuffer()&#x7B49;&#x4E00;&#x7CFB;&#x5217;</p>
<ul>
<li>&#x4E0D;&#x591A;&#x8BF4;&#xFF1A;<ul>
<li>ByteBuffer&#x5E95;&#x5C42;&#x662F;&#x4E00;&#x4E2A;byte[]&#xFF0C;get()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;byte&#xFF0C;1&#x5B57;&#x8282;&#xFF0C;8bit&#xFF0C;10&#x5B57;&#x8282;&#x53EF;&#x4EE5;get&#x51E0;&#x6B21;&#xFF1F;10&#x6B21;</li>
<li>ByteBuffer.asIntBuffer()&#x5F97;&#x5230;IntBuffer&#xFF0C;&#x5E95;&#x5C42;&#x662F;&#x4E00;&#x4E2A;int[]&#xFF0C;get()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;int&#xFF0C;&#x8FD8;&#x662F;10&#x5B57;&#x8282;&#xFF0C;&#x53EF;&#x4EE5;get&#x51E0;&#x6B21;&#xFF1F;</li>
<li>&#x540C;&#x7406;&#xFF0C;&#x8FD8;&#x6709;ShortBuffer, LongBuffer, FloatBuffer, DoubleBuffer&#xFF0C;&#x8FD9;&#x4E9B;&#x5C31;&#x662F;ByteBuffer&#x7684;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#xFF0C;&#x6240;&#x4EE5;&#x53EB;&#x89C6;&#x56FE;&#x7F13;&#x51B2;&#x5668;</li>
<li>asIntBuffer&#x65F6;&#xFF0C;&#x5982;&#x679C;ByteBuffer&#x672C;&#x8EAB;&#x6709;5&#x4E2A;byte&#xFF0C;&#x5219;&#x5176;&#x4E2D;&#x524D;4&#x4E2A;&#x4F1A;&#x53D8;&#x6210;IntBuffer&#x7684;&#x7B2C;0&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x7B2C;5&#x4E2A;&#x88AB;&#x5FFD;&#x7565;&#x4E86;&#xFF0C;&#x4F46;&#x5E76;&#x672A;&#x88AB;&#x4E22;&#x5F03;</li>
<li>&#x5F80;&#x65B0;&#x7684;IntBuffer&#x653E;&#x6570;&#x636E;&#xFF08;put(int)&#xFF09;&#xFF0C;&#x9ED8;&#x8BA4;&#x65F6;&#x4F1A;&#x4ECE;&#x5934;&#x5F00;&#x59CB;&#x5199;&#xFF0C;&#x5199;&#x5165;&#x7684;&#x6570;&#x636E;&#x4F1A;&#x53CD;&#x6620;&#x5230;&#x539F;&#x6765;&#x7684;ByteBuffer&#x4E0A;</li>
</ul>
</li>
</ul>
<ul>
<li>&#x603B;&#x7ED3;&#xFF1A;<ul>
<li><code>&#x5177;&#x4F53;&#x4E5F;&#x8BF4;&#x4E0D;&#x660E;&#x767D;&#x4E86;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4F60;&#x6709;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5C31;&#x7528;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#x7684;Buffer</code></li>
<li>&#x4F46;&#x76F4;&#x63A5;&#x5F80;&#x901A;&#x9053;&#x8BFB;&#x5199;&#x7684;&#xFF0C;&#x80AF;&#x5B9A;&#x662F;ByteBuffer&#xFF0C;&#x6240;&#x4EE5;&#x9996;&#x5148;&#x5F97;&#x6709;&#x4E2A;ByteBuffer&#xFF0C;&#x5176;&#x4ED6;&#x89C6;&#x56FE;Buffer&#xFF0C;&#x5C31;&#x5F97;&#x4ECE;ByteBuffer&#x6765;</li>
<li>&#x600E;&#x4E48;&#x4ECE;ByteBuffer&#x6765;&#x5462;&#xFF0C;ByteBuffer.asIntBuffer()&#x7B49;&#x65B9;&#x6CD5;</li>
</ul>
</li>
</ul>
<p>&#x4F8B;&#x5B50;&#xFF1A;ViewBuffers.java  </p>
<p><img src="https://raw.githubusercontent.com/cowthan/JavaAyo/master/doc/img/nio1.png" alt=""></p>
<h3 id="16-&#x5B57;&#x8282;&#x5E8F;">1.6 &#x5B57;&#x8282;&#x5E8F;</h3>
<ul>
<li>&#x7B80;&#x4ECB;&#xFF1A;<ul>
<li>&#x9AD8;&#x4F4D;&#x4F18;&#x5148;&#xFF0C;Big Endian&#xFF0C;&#x6700;&#x91CD;&#x8981;&#x7684;&#x5B57;&#x8282;&#x653E;&#x5730;&#x5740;&#x6700;&#x4F4E;&#x7684;&#x5B58;&#x50A8;&#x5355;&#x5143;&#xFF0C;ByteBuffer&#x9ED8;&#x8BA4;&#x4EE5;&#x9AD8;&#x4F4D;&#x4F18;&#x5148;&#xFF0C;&#x7F51;&#x7EDC;&#x4F20;&#x8F93;&#x5927;&#x90E8;&#x5206;&#x4E5F;&#x4EE5;&#x9AD8;&#x4F4D;&#x4F18;&#x5148;</li>
<li>&#x4F4E;&#x4F4D;&#x4F18;&#x5148;&#xFF0C;Little Endian</li>
<li>ByteBuffer.order()&#x65B9;&#x6CD5;&#x5207;&#x6362;&#x5B57;&#x8282;&#x5E8F;<ul>
<li>ByteOrderr.BIG_ENDIAN</li>
<li>ByteOrderr.LITTLE_ENDIAN</li>
</ul>
</li>
<li>&#x5BF9;&#x4E8E;00000000 01100001&#xFF0C;&#x6309;short&#x6765;&#x8BFB;&#xFF0C;&#x5982;&#x679C;&#x662F;big endian&#xFF0C;&#x5C31;&#x662F;97&#xFF0C; &#x4EE5;little endian&#xFF0C;&#x5C31;&#x662F;24832</li>
</ul>
</li>
</ul>
<h3 id="17-scattergather">1.7 Scatter/Gather</h3>
<p>&#x4E00;&#x4E2A;Channel&#xFF0C;&#x591A;&#x4E2A;Buffer&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x591A;&#x4E2A;&#x8FD0;&#x7164;&#x8F66;&#x5728;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#x5DE5;&#x4F5C;</p>
<p>&#x8BFB;&#x5230;&#x591A;&#x4E2A;Buffer&#x91CC;&#xFF1A;</p>
<pre><code class="lang-java">ByteBuffer header = ByteBuffer.allocate(<span class="hljs-number">128</span>);
ByteBuffer body   = ByteBuffer.allocate(<span class="hljs-number">1024</span>);
ByteBuffer[] bufferArray = { header, body };
channel.read(bufferArray);
</code></pre>
<p>&#x591A;&#x4E2A;Buffer&#x5F80;channel&#x5199;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-comment">//&#x6CE8;&#x610F;&#xFF0C;Buffer&#x7684;&#x957F;&#x5EA6;&#x662F;100&#xFF0C;&#x4F46;&#x53EA;&#x6709;50&#x4E2A;&#x6570;&#x636E;&#xFF0C;&#x5C31;&#x53EA;&#x4F1A;&#x5199;&#x5165;50&#xFF0C;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x53EA;&#x6709;position&#x548C;limit&#x4E4B;&#x95F4;&#x7684;&#x5185;&#x5BB9;&#x4F1A;&#x88AB;&#x5199;&#x5165;&#xFF08;put&#x5B8C;&#x5148;flip&#x4E00;&#x4E0B;&#xFF0C;&#x624D;&#x80FD;&#x5F80;channel&#x5199;&#xFF1F;&#xFF1F;&#xFF1F;&#xFF09;</span>
ByteBuffer header = ByteBuffer.allocate(<span class="hljs-number">128</span>);
ByteBuffer body   = ByteBuffer.allocate(<span class="hljs-number">1024</span>);
ByteBuffer[] bufferArray = { header, body };
channel.write(bufferArray);
</code></pre>
<h3 id="18-&#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;&#xFF1A;&#x5927;&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;">1.8 &#x5185;&#x5B58;&#x6620;&#x5C04;&#x6587;&#x4EF6;&#xFF1A;&#x5927;&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;</h3>
<p>&#x5927;&#x6587;&#x4EF6;&#xFF0C;&#x5982;2G&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x6CA1;&#x6CD5;&#x4E00;&#x4E0B;&#x52A0;&#x8F7D;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#x8BFB;&#x5199;</p>
<p>MappedByteBuffer&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x6620;&#x5C04;&#x529F;&#x80FD;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x6587;&#x4EF6;&#x90E8;&#x5206;&#x8F7D;&#x5165;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x4F46;&#x4F60;&#x4F7F;&#x7528;&#x65F6;&#xFF0C;
&#x611F;&#x89C9;&#x6587;&#x4EF6;&#x90FD;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x4E86;</p>
<p>MappedByteBuffer&#x7EE7;&#x627F;&#x4E86;ByteBuffer&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x50CF;&#x4E0A;&#x9762;&#x90A3;&#x6837;&#x4F7F;&#x7528;</p>
<p>MappedByteBuffer&#x6027;&#x80FD;&#x5F88;&#x9AD8;&#xFF0C;&#x8FDC;&#x9AD8;&#x4E8E;FileInputStream&#xFF0C;FileOutputStream&#xFF0C;RandomAccessFile&#x7684;&#x539F;&#x59CB;&#x65B9;&#x5F0F;&#x7684;&#x8BFB;&#x5199;&#xFF0C;&#x767E;&#x500D;&#x901F;&#x5EA6;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception </span>{

    <span class="hljs-comment">//&#x521B;&#x5EFA;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x5927;&#x5C0F;&#x662F;128M</span>
    MappedByteBuffer out = <span class="hljs-keyword">new</span> RandomAccessFile(<span class="hljs-string">&quot;test.dat&quot;</span>, <span class="hljs-string">&quot;rw&quot;</span>)
            .getChannel().map(FileChannel.MapMode.READ_WRITE, <span class="hljs-number">0</span>, length);

    <span class="hljs-comment">//&#x5199;&#x5165;</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; length; i++)
        out.put((<span class="hljs-keyword">byte</span>) <span class="hljs-string">&apos;x&apos;</span>);

    System.out.println(<span class="hljs-string">&quot;&#x5199;&#x5165;&#x5B8C;&#x6BD5;&quot;</span>);

    <span class="hljs-comment">//&#x8BFB;&#x53D6;</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = length / <span class="hljs-number">2</span>; i &lt; length / <span class="hljs-number">2</span> + <span class="hljs-number">6</span>; i++)
        System.out.println((<span class="hljs-keyword">char</span>) out.get(i));
}
</code></pre>
<h3 id="19-&#x6587;&#x4EF6;&#x52A0;&#x9501;">1.9 &#x6587;&#x4EF6;&#x52A0;&#x9501;</h3>
<ul>
<li>&#x7B80;&#x4ECB;<ul>
<li>&#x6709;&#x65F6;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x6587;&#x4EF6;&#x52A0;&#x9501;&#xFF0C;&#x4EE5;&#x540C;&#x6B65;&#x8BBF;&#x95EE;&#x67D0;&#x4E2A;&#x6587;&#x4EF6;</li>
<li>FileLock&#x662F;&#x4F7F;&#x7528;&#x4E86;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x7684;&#x6587;&#x4EF6;&#x52A0;&#x9501;&#x529F;&#x80FD;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x5F71;&#x54CD;&#x5230;&#x5176;&#x4ED6;&#x7CFB;&#x7EDF;&#x8FDB;&#x7A0B;&#xFF0C;&#x5176;&#x4ED6;&#x666E;&#x901A;&#x8FDB;&#x7A0B;&#xFF0C;&#x5373;&#x4F7F;&#x4E0D;&#x662F;java&#x5199;&#x7684;</li>
<li>FileLock.lock()&#x4F1A;&#x963B;&#x585E;&#xFF0C;tryLock&#x4E0D;&#x4F1A;&#x963B;&#x585E;</li>
<li>lock&#x7CFB;&#x5217;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5E26;&#x53C2;&#x6570;&#xFF1A;<ul>
<li>&#x52A0;&#x9501;&#x6587;&#x4EF6;&#x7684;&#x67D0;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x591A;&#x4E2A;&#x8FDB;&#x7A0B;&#x53EF;&#x4EE5;&#x5206;&#x522B;&#x52A0;&#x9501;&#x6587;&#x4EF6;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x5C31;&#x662F;&#x8FD9;&#x6837;</li>
<li>&#x53C2;&#x6570;3&#x53EF;&#x4EE5;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x8FD9;&#x91CC;&#x53C8;&#x51FA;&#x73B0;&#x4E2A;&#x5171;&#x4EAB;&#x9501;&#x548C;&#x72EC;&#x5360;&#x9501;&#xFF0C;&#x5171;&#x4EAB;&#x9501;&#x9700;&#x8981;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x652F;&#x6301;</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&#x7528;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception </span>{
    FileOutputStream fos = <span class="hljs-keyword">new</span> FileOutputStream(<span class="hljs-string">&quot;file.txt&quot;</span>);
    FileLock fl = fos.getChannel().tryLock();<span class="hljs-comment">//---------</span>
    <span class="hljs-keyword">if</span> (fl != <span class="hljs-keyword">null</span>) {
        System.out.println(<span class="hljs-string">&quot;Locked File&quot;</span>);
        TimeUnit.MILLISECONDS.sleep(<span class="hljs-number">100</span>);
        fl.release();<span class="hljs-comment">//---------------------------------</span>
        System.out.println(<span class="hljs-string">&quot;Released Lock&quot;</span>);
    }
    fos.close();
}
</code></pre>
<p>&#x66F4;&#x591A;&#x4F8B;&#x5B50;</p>
<pre><code>package com.cowthan.nio;

//: io/LockingMappedFiles.java
// Locking portions of a mapped file.
// {RunByHand}
import java.nio.*;
import java.nio.channels.*;
import java.io.*;

public class LockingMappedFiles {
    static final int LENGTH = 0x8FFFFFF; // 128 MB
    static FileChannel fc;

    public static void main(String[] args) throws Exception {
        fc = new RandomAccessFile(&quot;test.dat&quot;, &quot;rw&quot;).getChannel();
        MappedByteBuffer out = fc
                .map(FileChannel.MapMode.READ_WRITE, 0, LENGTH);
        for (int i = 0; i &lt; LENGTH; i++)
            out.put((byte) &apos;x&apos;);
        new LockAndModify(out, 0, 0 + LENGTH / 3);
        new LockAndModify(out, LENGTH / 2, LENGTH / 2 + LENGTH / 4);
    }

    private static class LockAndModify extends Thread {
        private ByteBuffer buff;
        private int start, end;

        LockAndModify(ByteBuffer mbb, int start, int end) {
            this.start = start;
            this.end = end;
            mbb.limit(end);
            mbb.position(start);
            buff = mbb.slice();
            start();
        }

        public void run() {
            try {
                // Exclusive lock with no overlap:
                FileLock fl = fc.lock(start, end, false);
                System.out.println(&quot;Locked: &quot; + start + &quot; to &quot; + end);
                // Perform modification:
                while (buff.position() &lt; buff.limit() - 1)
                    buff.put((byte) (buff.get() + 1));
                fl.release();
                System.out.println(&quot;Released: &quot; + start + &quot; to &quot; + end);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
} // /:~
</code></pre><h2 id="2-&#x5F02;&#x6B65;io">2 &#x5F02;&#x6B65;IO</h2>
<ul>
<li><p>&#x5173;&#x4E8E;Channel&#xFF1A;</p>
<ul>
<li>FileChannel&#xFF1A;&#x6C38;&#x8FDC;&#x90FD;&#x662F;&#x963B;&#x585E;&#x6A21;&#x5F0F;&#xFF0C;&#x5F53;&#x7136;&#x8BFB;&#x672C;&#x5730;&#x6587;&#x4EF6;&#x4E5F;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x591A;&#x4E45;&#xFF0C;&#x6CA1;&#x6CD5;&#x548C;Selector&#x914D;&#x5408;</li>
<li>DatagramChannel&#xFF1A;&#x901A;&#x8FC7;UDP&#x8BFB;&#x5199;&#x7F51;&#x7EDC;&#xFF0C;&#x65E0;&#x8FDE;&#x63A5;&#x7684;</li>
<li>SocketChannel&#xFF1A;&#x901A;&#x8FC7;TCP&#x8BFB;&#x5199;&#x7F51;&#x7EDC;</li>
<li>ServerSocketChannel&#xFF1A;&#x76D1;&#x542C;&#x65B0;&#x6765;&#x7684;TCP&#x8FDE;&#x63A5;&#xFF0C;&#x6BCF;&#x4E2A;&#x65B0;&#x8FDB;&#x6765;&#x7684;&#x8FDE;&#x63A5;&#x90FD;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;SocketChannel</li>
</ul>
</li>
<li><p>&#x7B80;&#x4ECB;&#xFF1A;</p>
<ul>
<li>Selector&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;&#x591A;&#x4E2A;Channel&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x4E0E;&#x4E4B;&#x76F8;&#x6BD4;&#xFF0C;&#x65E7;&#x7684;Socket&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x662F;&#x6BCF;&#x4E2A;Socket&#x8FDE;&#x63A5;&#x90FD;&#x5728;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E0A;&#x963B;&#x585E;</li>
<li>Channel&#x548C;Selector&#x914D;&#x5408;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;channel.configureBlocking(false)&#x5207;&#x6362;&#x5230;&#x975E;&#x963B;&#x585E;&#x6A21;&#x5F0F;</li>
<li>&#x800C;FileChannel&#x6CA1;&#x6709;&#x975E;&#x963B;&#x585E;&#x6A21;&#x5F0F;&#xFF0C;&#x53EA;&#x6709;Socket&#x76F8;&#x5173;&#x7684;Channel&#x624D;&#x6709;</li>
</ul>
</li>
</ul>
<ul>
<li>&#x6982;&#x62EC;&#xFF1A;<ul>
<li>SocketServerChannel&#x548C;SocketChannel&#x7684;&#x57FA;&#x672C;&#x7528;&#x6CD5;&#xFF0C;&#x53C2;&#x8003;socket.nio.NioXXServer&#x548C;Client</li>
<li>&#x53EF;&#x80FD;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;channel.configureBlocking(false)&#x8BBE;&#x7F6E;&#x975E;&#x963B;&#x585E;&#x7684;&#x5730;&#x65B9;&#xFF1A;<ul>
<li>SocketChannel.connect(new InetSocketAddress(hostname, port))&#xFF0C; &#x914D;&#x5408;sc.finishConnect()&#x5224;&#x65AD;&#x662F;&#x5426;&#x8FDE;&#x63A5;&#x6210;&#x529F;</li>
<li>SocketChannel sc = ssc.accept()&#xFF0C;&#x5728;&#x975E;&#x963B;&#x585E;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;&#x65E0;&#x65B0;&#x8FDE;&#x63A5;&#x8FDB;&#x6765;&#x65F6;&#x8FD4;&#x56DE;&#x503C;&#x4F1A;&#x662F;null</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="21-&#x65E7;io&#x5904;&#x7406;socket&#x7684;&#x65B9;&#x5F0F;">2.1 &#x65E7;IO&#x5904;&#x7406;Socket&#x7684;&#x65B9;&#x5F0F;</h3>
<p>&#x8981;&#x8BFB;&#x53D6;Socket&#x4E0A;&#x7684;Stream&#xFF0C;&#x5C31;&#x5F97;&#x5728;read&#x65F6;&#x963B;&#x585E;&#xFF0C;&#x6240;&#x4EE5;&#x6BCF;&#x4E00;&#x4E2A;Socket&#x90FD;&#x5F97;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;&#xFF0C;&#x5BF9;&#x4E8E;&#x670D;&#x52A1;&#x5668;&#x6765;&#x8BF4;&#xFF0C;&#x80FD;&#x5F00;&#x7684;&#x7EBF;&#x7A0B;&#x6570;&#x662F;&#x6709;&#x9650;&#x7684;</p>
<h3 id="22-&#x4E0D;&#x4F7F;&#x7528;selector&#xFF0C;&#x81EA;&#x5DF1;&#x60F3;&#x6CD5;&#x7BA1;&#x7406;socketchannel">2.2 &#x4E0D;&#x4F7F;&#x7528;Selector&#xFF0C;&#x81EA;&#x5DF1;&#x60F3;&#x6CD5;&#x7BA1;&#x7406;SocketChannel</h3>
<pre><code class="lang-java"><span class="hljs-meta">@Override</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">while</span>(!isClosed &amp;&amp; !Thread.interrupted()){
        <span class="hljs-keyword">for</span>(String key: map.keySet()){
            SocketChannel sc = map.get(key);

            ByteBuffer buf = ByteBuffer.allocate(<span class="hljs-number">1024</span>);
            <span class="hljs-keyword">try</span> {
                <span class="hljs-keyword">int</span> bytesRead = sc.read(buf);
                buf.flip();
                <span class="hljs-keyword">if</span>(bytesRead &lt;= <span class="hljs-number">0</span>){

                }<span class="hljs-keyword">else</span>{
                    System.out.println(<span class="hljs-string">&quot;&#x6536;&#x5230;&#x6D88;&#x606F;&#xFF08;&#x6765;&#x81EA;&quot;</span> + key + <span class="hljs-string">&quot;&#xFF09;&#xFF1A;&quot;</span> + Charset.forName(<span class="hljs-string">&quot;utf-8&quot;</span>).decode(buf));
                }
            } <span class="hljs-keyword">catch</span> (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
</code></pre>
<ul>
<li><p>&#x5F0A;&#x7AEF;&#x5206;&#x6790;&#xFF1A;</p>
<ul>
<li>&#x4E0D;&#x65AD;&#x5FAA;&#x73AF;&#x8BFB;&#x53D6;&#x6240;&#x6709;Channel&#xFF0C;&#x6709;&#x6570;&#x636E;&#x5219;&#x8BFB;&#x51FA;&#x6765;</li>
<li>&#x95EE;&#x9898;1&#xFF1A;&#x5728;while&#x91CC;&#xFF0C;&#x4F60;sleep&#x8FD8;&#x662F;&#x4E0D;sleep&#xFF0C;sleep&#x5C31;&#x635F;&#x5931;&#x592A;&#x591A;&#x5B9E;&#x65F6;&#x6027;&#xFF0C;&#x4E0D;sleep&#x5C31;&#x5BFC;&#x81F4;CPU&#x5927;&#x91CF;&#x7A7A;&#x8F6C;</li>
<li>&#x95EE;&#x9898;2&#xFF1A;&#x5BF9;&#x4E8E;ServerSocketChannel&#xFF0C;&#x5982;&#x679C;accept&#x975E;&#x963B;&#x585E;&#xFF0C;&#x5219;&#x9700;&#x8981;while(true)&#x4E0D;&#x65AD;&#x5224;&#x65AD;&#x662F;&#x5426;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#xFF0C;&#x4E5F;&#x6D6A;&#x8D39;CPU</li>
<li>&#x95EE;&#x9898;3&#xFF1A;&#x5BF9;&#x4E8E;ServerSocket.connect()&#xFF0C;&#x5982;&#x679C;&#x975E;&#x963B;&#x585E;&#xFF0C;&#x5219;&#x9700;&#x8981;while(true)&#x4E0D;&#x65AD;&#x5224;&#x65AD;&#x662F;&#x5426;&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x5668;&#x6210;&#x529F;&#xFF0C;&#x4E5F;&#x6D6A;&#x8D39;CPU</li>
</ul>
</li>
<li><p>&#x6240;&#x4EE5;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x9700;&#x8981;&#x4EC0;&#x4E48;&#x4E86;</p>
<ul>
<li>&#x9700;&#x8981;SocketChannel&#x7684;read&#x65B9;&#x6CD5;&#x4E0D;&#x963B;&#x585E;</li>
<li>&#x6216;&#x8005;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x4E1C;&#x897F;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6240;&#x6709;SocketChannel&#x4E0A;&#x7B49;&#x5F85;&#xFF0C;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x6709;&#x4E86;&#x6D88;&#x606F;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5524;&#x9192;&#xFF0C;&#x8FD9;&#x91CC;&#x9762;&#x5C31;&#x6709;&#x4E2A;&#x76D1;&#x542C;&#x7684;&#x6982;&#x5FF5;</li>
<li>&#x5E76;&#x4E14;&#x53EF;&#x8BFB;&#xFF0C;&#x53EF;&#x5199;&#xFF0C;accept(), connect()&#x90FD;&#x5E94;&#x8BE5;&#x5BF9;&#x5E94;&#x4E0D;&#x540C;&#x7684;&#x4E8B;&#x4EF6;</li>
<li>&#x8FD9;&#x5C31;&#x5F15;&#x51FA;&#x4E86;Selector&#xFF0C;Selector&#x5C31;&#x662F;java&#x4ECE;&#x8BED;&#x8A00;&#x5C42;&#x9762;&#x548C;&#x7CFB;&#x7EDF;&#x5C42;&#x9762;&#x5BF9;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;</li>
</ul>
</li>
</ul>
<h3 id="23-selector">2.3 Selector</h3>
<p>&#x4F7F;&#x7528;Selector&#x7684;&#x5B8C;&#x6574;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-java">Selector selector = Selector.open();
channel.configureBlocking(<span class="hljs-keyword">false</span>);
SelectionKey key = channel.register(selector, SelectionKey.OP_READ);
<span class="hljs-keyword">while</span>(<span class="hljs-keyword">true</span>) {
  <span class="hljs-keyword">int</span> readyChannels = selector.select();   <span class="hljs-comment">//&#x5C31;&#x5728;&#x8FD9;&#x963B;&#x585E;&#xFF0C;&#x4F46;&#x5DF2;&#x7ECF;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;&#x591A;&#x4E2A;Channel&#xFF08;SocketChannel-&#x8BFB;&#x5199;&#xFF0C;connect&#x4E8B;&#x4EF6;&#xFF0C;DatagramChannel-&#x8BFB;&#x5199;&#x4E8B;&#x4EF6;&#xFF0C;SocketServerChannel-accept&#x4E8B;&#x4EF6;&#xFF09;</span>
  <span class="hljs-keyword">if</span>(readyChannels == <span class="hljs-number">0</span>) <span class="hljs-keyword">continue</span>;
  Set selectedKeys = selector.selectedKeys();
  Iterator keyIterator = selectedKeys.iterator();
  <span class="hljs-keyword">while</span>(keyIterator.hasNext()) {
    SelectionKey key = keyIterator.next();
    <span class="hljs-keyword">if</span>(key.isAcceptable()) {
        <span class="hljs-comment">// a connection was accepted by a ServerSocketChannel.</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key.isConnectable()) {
        <span class="hljs-comment">// a connection was established with a remote server.</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key.isReadable()) {
        <span class="hljs-comment">// a channel is ready for reading</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key.isWritable()) {
        <span class="hljs-comment">// a channel is ready for writing</span>
    }
    keyIterator.remove();
  }
}
</code></pre>
<pre><code class="lang-java">Selector selector = Selector.open();
SelectionKey selectionKey = sc.register(selector, SelectionKey.OP_READ);

<span class="hljs-comment">//&#x770B;Selector&#x5BF9;&#x54EA;&#x4E9B;&#x4E8B;&#x4EF6;&#x611F;&#x5174;&#x8DA3;</span>
<span class="hljs-keyword">int</span> interestSet = selectionKey.interestOps();
<span class="hljs-keyword">boolean</span> isInterestedInAccept  = (interestSet &amp; SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;
<span class="hljs-keyword">boolean</span> isInterestedInConnect = interestSet &amp; SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT;
<span class="hljs-keyword">boolean</span> isInterestedInRead    = interestSet &amp; SelectionKey.OP_READ) == SelectionKey.OP_READ;
<span class="hljs-keyword">boolean</span> isInterestedInWrite   = interestSet &amp; SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE;

<span class="hljs-comment">//&#x901A;&#x9053;&#x4E2D;&#x5DF2;&#x7ECF;&#x5C31;&#x7EEA;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x6BCF;&#x4E00;&#x6B21;selection&#x90FD;&#x5F97;&#x5148;&#x8BBF;&#x95EE;&#x8FD9;&#x4E2A;&#xFF0C;&#x77E5;&#x9053;&#x662F;&#x56E0;&#x4E3A;&#x54EA;&#x4E9B;&#x4E8B;&#x4EF6;&#x88AB;&#x5524;&#x9192;&#x7684;</span>
<span class="hljs-keyword">int</span> readySet = selectionKey.readyOps();
<span class="hljs-comment">//&#x6216;&#x8005;&#xFF1A;</span>
selectionKey.isAcceptable();
selectionKey.isConnectable();
selectionKey.isReadable();
selectionKey.isWritable();

<span class="hljs-comment">//&#x62FF;&#x5230;Channel&#x548C;Selector</span>
Channel  channel  = selectionKey.channel();
Selector selector = selectionKey.selector();

<span class="hljs-comment">//&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#x662F;&#xFF1A;1&#x4E2A;Selector&#xFF0C;&#x591A;&#x4E2A;Channel&#xFF0C;&#x591A;&#x4E2A;SelectionKey&#xFF0C;&#x4E00;&#x4E2A;Channel&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;SelectionKey&#xFF0C;&#x800C;&#x4E14;&#x4E00;&#x4E2A;SelectionKey&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;extra&#x6570;&#x636E;&#xFF0C;&#x4EE5;&#x6EE1;&#x8DB3;&#x7279;&#x5B9A;&#x9700;&#x6C42;</span>

<span class="hljs-comment">//select&#x65B9;&#x6CD5;&#xFF1A;&#x8FD9;&#x624D;&#x662F;&#x4F1A;&#x963B;&#x585E;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x963B;&#x585E;&#xFF0C;&#x662F;&#x6027;&#x80FD;&#x6700;&#x4F73;&#x7684;&#x8868;&#x73B0;</span>
<span class="hljs-keyword">int</span> readyCount = selector.select()  <span class="hljs-comment">//select()&#x963B;&#x585E;&#x5230;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#x5728;&#x4F60;&#x6CE8;&#x518C;&#x7684;&#x4E8B;&#x4EF6;&#x4E0A;&#x5C31;&#x7EEA;&#x4E86;</span>
<span class="hljs-keyword">int</span> readyCount = selector.select(<span class="hljs-keyword">long</span> timeout) <span class="hljs-comment">//&#x6700;&#x957F;&#x4F1A;&#x963B;&#x585E;timeout&#x6BEB;&#x79D2;(&#x53C2;&#x6570;)</span>
<span class="hljs-keyword">int</span> readyCount = selector.selectNow() <span class="hljs-comment">//&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x65E0;&#x5219;0</span>
<span class="hljs-comment">//&#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x6709;&#x51E0;&#x4E2A;&#x901A;&#x9053;&#x5C31;&#x7EEA;</span>
<span class="hljs-comment">/*
select()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;int&#x503C;&#x8868;&#x793A;&#x6709;&#x591A;&#x5C11;&#x901A;&#x9053;&#x5DF2;&#x7ECF;&#x5C31;&#x7EEA;&#x3002;&#x4EA6;&#x5373;&#xFF0C;&#x81EA;&#x4E0A;&#x6B21;&#x8C03;&#x7528;select()&#x65B9;&#x6CD5;&#x540E;&#x6709;&#x591A;&#x5C11;&#x901A;
&#x9053;&#x53D8;&#x6210;&#x5C31;&#x7EEA;&#x72B6;&#x6001;&#x3002;&#x5982;&#x679C;&#x8C03;&#x7528;select()&#x65B9;&#x6CD5;&#xFF0C;&#x56E0;&#x4E3A;&#x6709;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#x53D8;&#x6210;&#x5C31;&#x7EEA;&#x72B6;&#x6001;&#xFF0C;&#x8FD4;&#x56DE;&#x4E86;1&#xFF0C;&#x82E5;&#x518D;&#x6B21;&#x8C03;&#x7528;select()&#x65B9;&#x6CD5;&#xFF0C;
&#x5982;&#x679C;&#x53E6;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#x5C31;&#x7EEA;&#x4E86;&#xFF0C;&#x5B83;&#x4F1A;&#x518D;&#x6B21;&#x8FD4;&#x56DE;1&#x3002;&#x5982;&#x679C;&#x5BF9;&#x7B2C;&#x4E00;&#x4E2A;&#x5C31;&#x7EEA;&#x7684;channel&#x6CA1;&#x6709;&#x505A;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;&#xFF0C;&#x73B0;&#x5728;&#x5C31;&#x6709;&#x4E24;&#x4E2A;&#x5C31;&#x7EEA;&#x7684;&#x901A;
&#x9053;&#xFF0C;&#x4F46;&#x5728;&#x6BCF;&#x6B21;select()&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x4E4B;&#x95F4;&#xFF0C;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x901A;&#x9053;&#x5C31;&#x7EEA;&#x4E86;
*/</span>

<span class="hljs-comment">//&#x6709;&#x901A;&#x9053;&#x5C31;&#x7EEA;&#x4E86;&#xFF0C;&#x5C31;&#x5F97;&#x5F97;&#x5230;&#x8FD9;&#x4E2A;Channel&#xFF0C;&#x901A;&#x9053;&#x5B58;&#x5728;SelectionKey&#x91CC;&#xFF0C;&#x800C;selector&#x53EF;&#x4EE5;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;SelectionKey&#x96C6;&#x5408;</span>
Set selectedKeys = selector.selectedKeys();
Iterator keyIterator = selectedKeys.iterator();
<span class="hljs-keyword">while</span>(keyIterator.hasNext()) {
    SelectionKey key = keyIterator.next();
    <span class="hljs-keyword">if</span>(key.isAcceptable()) {
        <span class="hljs-comment">// a connection was accepted by a ServerSocketChannel.</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key.isConnectable()) {
        <span class="hljs-comment">// a connection was established with a remote server.</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key.isReadable()) {
        <span class="hljs-comment">// a channel is ready for reading</span>
        Channel channel = key.channel();
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key.isWritable()) {
        <span class="hljs-comment">// a channel is ready for writing</span>
    }
    keyIterator.remove();
}
</code></pre>
<ul>
<li><p>register&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#xFF1A;Channel&#x4E8B;&#x4EF6;</p>
<ul>
<li>&#x53C2;&#x6570;&#x8868;&#x793A;Selector&#x5BF9;Channel&#x7684;&#x4EC0;&#x4E48;&#x4E8B;&#x4EF6;&#x611F;&#x5174;&#x8DA3;</li>
<li>Connect&#xFF1A;SelectionKey.OP_CONNECT</li>
<li>Accept&#xFF1A;SelectionKey.OP_ACCEPT</li>
<li>Read&#xFF1A;SelectionKey.OP_READ</li>
<li>Write&#xFF1A;SelectionKey.OP_WRITE</li>
<li>&#x53EF;&#x4EE5;&#x7EC4;&#x5408;&#xFF1A;int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;</li>
</ul>
</li>
<li><p>SelectionKey&#x90FD;&#x6709;&#x5565;&#x4FE1;&#x606F;&#xFF1A;</p>
<ul>
<li>interest&#x96C6;&#x5408;&#xFF1A;&#x5BF9;&#x54EA;&#x4E9B;&#x4E8B;&#x4EF6;&#x611F;&#x5174;&#x8DA3;</li>
<li>ready&#x96C6;&#x5408;&#xFF1A;&#x611F;&#x5174;&#x8DA3;&#x7684;&#x4E8B;&#x4EF6;&#x96C6;&#x4E2D;&#xFF0C;&#x54EA;&#x4E9B;&#x4E8B;&#x4EF6;&#x51C6;&#x5907;&#x5C31;&#x7EEA;&#x4E86;</li>
<li>Channel&#xFF1A;&#x76D1;&#x542C;&#x7684;&#x54EA;&#x4E2A;Channel</li>
<li>Selector&#xFF1A;&#x8C01;&#x5728;&#x76D1;&#x542C;</li>
<li>&#x53EF;&#x9009;&#x7684;extra</li>
</ul>
</li>
</ul>
<ul>
<li>&#x5524;&#x9192;&#x963B;&#x585E;&#x7684;Selector&#xFF1A;&#x5728;select&#x65B9;&#x6CD5;&#x7684;&#x963B;&#x585E;<ul>
<li>&#x60C5;&#x51B5;1&#xFF1A;&#x6709;&#x611F;&#x5174;&#x8DA3;&#x7684;&#x4E8B;&#x4EF6;&#x6765;&#x4E86;</li>
<li>&#x60C5;&#x51B5;2&#xFF1A;&#x624B;&#x52A8;&#x8C03;&#x7528;Selector.wakeup()&#xFF0C;&#x53EA;&#x8981;&#x8BA9;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#x5728;&#x7B2C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;select()&#x65B9;&#x6CD5;&#x7684;&#x90A3;&#x4E2A;&#x5BF9;&#x8C61;&#x4E0A;&#x8C03;&#x7528;Selector.wakeup()&#x65B9;&#x6CD5;&#x5373;&#x53EF;<ul>
<li>&#x5982;&#x679C;&#x6709;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;&#x4E86;wakeup()&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x5F53;&#x524D;&#x6CA1;&#x6709;&#x7EBF;&#x7A0B;&#x963B;&#x585E;&#x5728;select()&#x65B9;&#x6CD5;&#x4E0A;&#xFF0C;&#x4E0B;&#x4E2A;&#x8C03;&#x7528;select()&#x65B9;&#x6CD5;&#x7684;&#x7EBF;&#x7A0B;&#x4F1A;&#x7ACB;&#x5373;&#x201C;&#x9192;&#x6765;&#xFF08;wake up&#xFF09;&#x201D;&#x3002;</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>&#x5173;&#x95ED;Selector<ul>
<li>close()&#x65B9;&#x6CD5;&#xFF0C;&#x5173;&#x95ED;&#x8BE5;Selector&#xFF0C;&#x4E14;&#x4F7F;&#x6CE8;&#x518C;&#x5230;&#x8BE5;Selector&#x4E0A;&#x7684;&#x6240;&#x6709;SelectionKey&#x5B9E;&#x4F8B;&#x65E0;&#x6548;</li>
<li>&#x901A;&#x9053;&#x672C;&#x8EAB;&#x5E76;&#x4E0D;&#x4F1A;&#x5173;&#x95ED;</li>
</ul>
</li>
</ul>
<h2 id="3-datagramchannel&#xFF1A;udp&#x901A;&#x4FE1;">3 DatagramChannel&#xFF1A;UDP&#x901A;&#x4FE1;</h2>
<p>&#x53C2;&#x8003;&#xFF1A;<a href="http://ifeve.com/datagram-channel/" target="_blank">http://ifeve.com/datagram-channel/</a></p>
<p>Java NIO&#x4E2D;&#x7684;DatagramChannel&#x662F;&#x4E00;&#x4E2A;&#x80FD;&#x6536;&#x53D1;UDP&#x5305;&#x7684;&#x901A;&#x9053;&#x3002;&#x56E0;&#x4E3A;UDP&#x662F;&#x65E0;&#x8FDE;&#x63A5;&#x7684;&#x7F51;&#x7EDC;&#x534F;&#x8BAE;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x50CF;&#x5176;&#x5B83;&#x901A;&#x9053;&#x90A3;&#x6837;&#x8BFB;&#x53D6;&#x548C;&#x5199;&#x5165;&#x3002;&#x5B83;&#x53D1;&#x9001;&#x548C;&#x63A5;&#x6536;&#x7684;&#x662F;&#x6570;&#x636E;&#x5305;&#x3002;</p>
<pre><code class="lang-java">
<span class="hljs-comment">//&#x6253;&#x5F00;&#x4E00;&#x4E2A;DatagramChannel&#xFF0C;&#x53EF;&#x4EE5;&#x5728;UDP&#x7AEF;&#x53E3;9999&#x4E0A;&#x63A5;&#x6536;&#x6570;&#x636E;&#x5305;</span>
DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(<span class="hljs-keyword">new</span> InetSocketAddress(<span class="hljs-number">9999</span>));

<span class="hljs-comment">//&#x63A5;&#x6536;&#x6570;&#x636E;</span>
ByteBuffer buf = ByteBuffer.allocate(<span class="hljs-number">48</span>);
buf.clear();
channel.receive(buf);

<span class="hljs-comment">//&#x53D1;&#x9001;&#x6570;&#x636E;</span>
String newData = <span class="hljs-string">&quot;New String to write to file...&quot;</span> + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(<span class="hljs-number">48</span>);
buf.clear();
buf.put(newData.getBytes());
buf.flip();

<span class="hljs-keyword">int</span> bytesSent = channel.send(buf, <span class="hljs-keyword">new</span> InetSocketAddress(<span class="hljs-string">&quot;jenkov.com&quot;</span>, <span class="hljs-number">80</span>));

<span class="hljs-comment">//&#x8FDE;&#x63A5;&#x5230;&#x7279;&#x5B9A;&#x5730;&#x5740;</span>
&#x53EF;&#x4EE5;&#x5C06;DatagramChannel&#x201C;&#x8FDE;&#x63A5;&#x201D;&#x5230;&#x7F51;&#x7EDC;&#x4E2D;&#x7684;&#x7279;&#x5B9A;&#x5730;&#x5740;&#x7684;&#x3002;&#x7531;&#x4E8E;UDP&#x662F;&#x65E0;&#x8FDE;&#x63A5;&#x7684;&#xFF0C;&#x8FDE;&#x63A5;&#x5230;
&#x7279;&#x5B9A;&#x5730;&#x5740;&#x5E76;&#x4E0D;&#x4F1A;&#x50CF;TCP&#x901A;&#x9053;&#x90A3;&#x6837;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x771F;&#x6B63;&#x7684;&#x8FDE;&#x63A5;&#x3002;&#x800C;&#x662F;&#x9501;&#x4F4F;DatagramChannel &#xFF0C;
&#x8BA9;&#x5176;&#x53EA;&#x80FD;&#x4ECE;&#x7279;&#x5B9A;&#x5730;&#x5740;&#x6536;&#x53D1;&#x6570;&#x636E;&#x3002;
channel.connect(<span class="hljs-keyword">new</span> InetSocketAddress(<span class="hljs-string">&quot;jenkov.com&quot;</span>, <span class="hljs-number">80</span>));
<span class="hljs-keyword">int</span> bytesRead = channel.read(buf);
<span class="hljs-keyword">int</span> bytesWritten = channel.write(but);
</code></pre>
<h2 id="4-pipe">4 Pipe</h2>
<p><a href="http://ifeve.com/pipe/" target="_blank">http://ifeve.com/pipe/</a></p>
<ul>
<li>&#x7B80;&#x4ECB;&#xFF1A;<ul>
<li>Pipe&#x7528;&#x4E8E;&#x7EBF;&#x7A0B;&#x901A;&#x4FE1;&#xFF0C;&#x4E24;&#x4E2A;Thread&#x7531;&#x4E00;&#x4E2A;Pipe&#x8FDE;&#x63A5;</li>
<li>pipe&#x7684;&#x4E24;&#x7AEF;&#xFF0C;&#x4E00;&#x7AEF;&#x662F;SinkChannel&#xFF0C;&#x8D1F;&#x8D23;&#x5199;&#x5165;&#xFF0C;&#x4E00;&#x7AEF;&#x662F;SourceChannel&#xFF0C;&#x8D1F;&#x8D23;&#x8BFB;&#x53D6;</li>
<li>&#x6240;&#x4EE5;pipe&#x662F;&#x5355;&#x5411;&#x901A;&#x4FE1;</li>
<li>&#x4E24;&#x4E2A;Pipe&#x5C31;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x53CC;&#x5411;&#x901A;&#x4FE1;</li>
</ul>
</li>
</ul>
<p>&#x770B;&#x56FE;&#xFF1A;<br><img src="https://raw.githubusercontent.com/cowthan/JavaAyo/master/doc/img/pipe.bmp" alt=""></p>
<pre><code class="lang-java">Pipe pipe = Pipe.open();

<span class="hljs-comment">//&#x5199;&#x5165;</span>
Pipe.SinkChannel sinkChannel = pipe.sink();
String newData = <span class="hljs-string">&quot;New String to write to file...&quot;</span> + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(<span class="hljs-number">48</span>);
buf.clear();
buf.put(newData.getBytes());

buf.flip();

<span class="hljs-keyword">while</span>(buf.hasRemaining()) {
    sinkChannel.write(buf);
}

<span class="hljs-comment">//&#x8BFB;&#x53D6;</span>
Pipe.SourceChannel sourceChannel = pipe.source();
ByteBuffer buf = ByteBuffer.allocate(<span class="hljs-number">48</span>);
<span class="hljs-keyword">int</span> bytesRead = sourceChannel.read(buf);
</code></pre>
<h2 id="5-okio">5 Okio</h2>
<p>github: <a href="https://github.com/square/okio" target="_blank">https://github.com/square/okio</a></p>
<p>&#x53EA;&#x662F;&#x5BF9;&#x65E7;IO&#x7684;&#x5C01;&#x88C5;&#xFF0C;&#x6CA1;&#x7528;&#x5230;Channel&#xFF0C;&#x4E5F;&#x6CA1;&#x7528;&#x5230;ByteBuffer</p>
<h3 id="51-&#x7B80;&#x4ECB;&#xFF1A;">5.1 &#x7B80;&#x4ECB;&#xFF1A;</h3>
<ul>
<li>&#x57FA;&#x672C;&#x63A5;&#x53E3;<ul>
<li>Source&#xFF1A;&#x63A5;&#x53E3;&#xFF0C;like InputStream <ul>
<li>&#x8F93;&#x5165;&#x6D41;&#xFF0C;&#x8F93;&#x5165;&#x5230;&#x5185;&#x5B58;&#xFF0C;&#x4ECE;Source&#x8BFB;</li>
<li>long read(Buffer sink, long byteCount) throws IOException<ul>
<li>&#x8FD4;&#x56DE;-1&#x8868;&#x793A;EOF&#xFF0C;&#x5199;&#x5230;sink&#x91CC;</li>
</ul>
</li>
<li>Timeout timeout()<ul>
<li>&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;source&#x7684;&#x8D85;&#x65F6;&#x4FE1;&#x606F;</li>
</ul>
</li>
<li>void close() throws IOException</li>
</ul>
</li>
<li>Sink&#xFF1A;&#x63A5;&#x53E3;&#xFF0C;like OutputStream<ul>
<li>&#x8F93;&#x51FA;&#x6D41;&#xFF0C;&#x4ECE;&#x5185;&#x5B58;&#x8F93;&#x51FA;&#xFF0C;&#x5F80;Sink&#x5199;</li>
<li>void write(Buffer source, long byteCount) throws IOException<ul>
<li>&#x4ECE;source&#x8BFB;&#x5230;sink</li>
</ul>
</li>
<li>Timeout timeout();</li>
<li>void close() throws IOException;</li>
</ul>
</li>
<li>BufferedSource&#xFF1A;&#x63A5;&#x53E3;&#xFF0C;extends Source<ul>
<li>&#x8F93;&#x51FA;&#x7F13;&#x51B2;&#x6D41;</li>
<li>&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x8BFB;&#x65B9;&#x6CD5;</li>
<li>&#x5B9E;&#x73B0;&#x7C7B;&#xFF1A;RealBufferedSource&#xFF0C;&#x9700;&#x8981;&#x4F20;&#x5165;&#x4E00;&#x4E2A;Source&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x88C5;&#x7C7B;</li>
</ul>
</li>
<li>BufferedSink&#xFF1A;&#x63A5;&#x53E3;&#xFF0C;extends sink<ul>
<li>&#x8F93;&#x5165;&#x7F13;&#x51B2;&#x6D41;</li>
<li>&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x5199;&#x65B9;&#x6CD5;</li>
<li>&#x5B9E;&#x73B0;&#x7C7B;&#xFF1A;RealBufferedSink&#xFF0C;&#x9700;&#x8981;&#x4F20;&#x5165;&#x4E00;&#x4E2A;Sink&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x88C5;&#x7C7B;</li>
</ul>
</li>
<li>Sink&#x548C;Source&#x53EA;&#x6709;3&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x5B9E;&#x73B0;&#x65B9;&#x4FBF;&#xFF0C;&#x800C;BufferedSource&#x548C;BufferedSink&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x5806;&#x4FBF;&#x5229;&#x65B9;&#x6CD5;</li>
<li>Timeout&#xFF1A;&#x8BFB;&#x5199;&#x65F6;&#x6709;Timeout&#xFF0C;&#x4E3B;&#x8981;&#x7ED9;Socket&#x7528;</li>
<li>byte stream&#x548C;char stream&#x7684;&#x8BFB;&#x5199;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x533A;&#x522B;&#xFF0C;&#x5F53;&#x505A;byte[], utf8 String&#xFF0C;big-endian,little-endian&#x90FD;&#x884C;&#xFF0C;&#x4E0D;&#x518D;&#x7528;InputStreamReader&#x4E86;</li>
<li>Easy to test. The Buffer class implements both BufferedSource and BufferedSink so your test code is simple and clear.</li>
<li>&#x4E92;&#x64CD;&#x4F5C;&#xFF1A;Source&#x548C;InputStream&#x53EF;&#x4EE5;&#x4E92;&#x6362;&#xFF0C;Sink&#x548C;OutputStream&#x53EF;&#x4EE5;&#x4E92;&#x6362;&#xFF0C;&#x65E0;&#x7F1D;&#x517C;&#x5BB9;</li>
</ul>
</li>
</ul>
<ul>
<li><p>&#x5B9E;&#x7528;&#x7C7B;&#xFF1A;</p>
<ul>
<li>DeflaterSink&#xFF0C;InflaterSource</li>
<li>ForwardingSink&#xFF0C;ForwardingSource</li>
<li>GzipSink&#xFF0C;GzipSource</li>
<li>HashingSink&#xFF0C;HashingSource</li>
</ul>
</li>
<li><p>ByteString&#x548C;Buffer</p>
<ul>
<li>ByteString&#xFF1A;&#x5904;&#x7406;&#x5B57;&#x7B26;&#x4E32;<ul>
<li>&#x4E00;&#x4E2A;&#x4E0D;&#x53EF;&#x53D8;&#x7684;byte&#x5E8F;&#x5217;&#xFF0C;immutable sequence of bytes</li>
<li>String&#x662F;&#x57FA;&#x672C;&#x7684;&#xFF0C;ByteString&#x662F;String&#x7684;long lost brother</li>
<li>&#x63D0;&#x4F9B;&#x4FBF;&#x5229;&#x65B9;&#x6CD5;&#x5904;&#x7406;byte</li>
<li>&#x80FD;decode&#x548C;encode&#xFF0C;&#x5904;&#x7406;hex, base64, and UTF-8</li>
</ul>
</li>
<li>Buffer&#xFF1A;&#x5904;&#x7406;byte&#x6D41;<ul>
<li>&#x4E00;&#x4E2A;&#x53EF;&#x53D8;&#x7684;byte&#x5E8F;&#x5217;&#xFF0C;mutable sequence of bytes&#xFF0C;&#x50CF;&#x4E2A;ArrayList</li>
<li>&#x8BFB;&#x5199;&#x65F6;&#x884C;&#x4E3A;&#x50CF;Queue&#xFF0C;write to end&#xFF0C;read from front</li>
<li>&#x4E0D;&#x9700;&#x8981;&#x8003;&#x8651;&#x5927;&#x5C0F;&#xFF0C;&#x5C4F;&#x853D;&#x4E86;ByteBuffer&#x7684;capacity&#xFF0C;limit&#xFF0C;position&#x7B49;</li>
</ul>
</li>
<li>&#x7F13;&#x5B58;&#xFF1A;&#x628A;&#x4E00;&#x4E2A;utf-8 String decode&#x6210;ByteString&#xFF0C;&#x4F1A;&#x7F13;&#x5B58;&#xFF0C;&#x4E0B;&#x6B21;&#x518D;decode&#xFF0C;&#x5C31;&#x5FEB;&#x4E86;</li>
<li>Buffer&#x662F;&#x4E00;&#x4E2A;Segment&#x7684;LinkedList&#xFF0C;&#x6240;&#x4EE5;&#x62F7;&#x8D1D;&#x4E0D;&#x662F;&#x771F;&#x7684;&#x62F7;&#x8D1D;&#xFF0C;&#x53EA;&#x662F;&#x79FB;&#x52A8;&#xFF0C;&#x6240;&#x4EE5;&#x66F4;&#x5FEB;<ul>
<li>&#x591A;&#x7EBF;&#x7A0B;&#x5DE5;&#x4F5C;&#x65F6;&#x5C31;&#x6709;&#x4F18;&#x52BF;&#x4E86;&#xFF0C;&#x8FDE;&#x63A5;network&#x7684;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x8FC5;&#x901F;&#x7684;&#x628A;&#x6570;&#x636E;&#x53D1;&#x7ED9;work&#x7EBF;&#x7A0B;&#xFF08;without any copying or ceremony&#xFF09;</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>&#x5DE5;&#x5177;</p>
<ul>
<li>AsyncTimeout</li>
<li>Base64</li>
<li>Options</li>
<li>Timeout</li>
<li>Util</li>
<li>Okio</li>
</ul>
</li>
<li><p>Segment&#x76F8;&#x5173;</p>
<ul>
<li>Segment</li>
<li>SegmentPool</li>
<li>SegmentedByteString</li>
</ul>
</li>
</ul>
<h3 id="52-&#x4F7F;&#x7528;">5.2 &#x4F7F;&#x7528;</h3>
<h4 id="&#x6784;&#x9020;bufferedsink&#x548C;bufferedsource">&#x6784;&#x9020;BufferedSink&#x548C;BufferedSource</h4>
<pre><code class="lang-java"><span class="hljs-comment">//&#x521B;&#x5EFA;Source</span>
Source source = Okio.source(<span class="hljs-keyword">final</span> InputStream in, <span class="hljs-keyword">final</span> Timeout timeout);
source(InputStream in); <span class="hljs-comment">//new Timeout()</span>
source(File file);
source(Path path, OpenOption... options); <span class="hljs-comment">//java7</span>
source(Socket socket);

<span class="hljs-comment">//&#x521B;&#x5EFA;Sink</span>
Sink sink = Okio.sink(OutputStream out);
sink(<span class="hljs-keyword">final</span> OutputStream out, <span class="hljs-keyword">final</span> Timeout timeout);
sink(File file)
appendingSink(File file)
sink(Path path, OpenOption... options)
sink(Socket socket)

<span class="hljs-comment">//&#x521B;&#x5EFA;BufferedSource&#xFF1A;</span>
BufferedSource pngSource = Okio.buffer(Source source); <span class="hljs-comment">//&#x8FD4;&#x56DE;RealBufferedSource&#x5BF9;&#x8C61;</span>
BufferedSink pngSink = Okio.buffer(Sink sink); <span class="hljs-comment">//&#x8FD4;&#x56DE;RealBufferedSink&#x5BF9;&#x8C61;</span>

<span class="hljs-comment">//&#x4ECE;BufferedSource&#x8BFB;&#x53D6;</span>
&#x770B;&#x4F8B;&#x5B50;&#x5427;

<span class="hljs-comment">//&#x5F80;BufferedSink&#x5199;&#x5165;</span>
&#x770B;&#x4F8B;&#x5B50;&#x5427;

<span class="hljs-comment">//ByteString</span>
&#x770B;&#x4F8B;&#x5B50;&#x5427;


<span class="hljs-comment">//Buffer</span>
&#x770B;&#x4F8B;&#x5B50;&#x5427;
</code></pre>
<h3 id="53-&#x4F8B;&#x5B50;&#xFF1A;&#x6765;&#x81EA;&#x5B98;&#x7F51;">5.3 &#x4F8B;&#x5B50;&#xFF1A;&#x6765;&#x81EA;&#x5B98;&#x7F51;</h3>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.cowthan.nio.okio;

<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.io.InputStream;

<span class="hljs-keyword">import</span> okio.Buffer;
<span class="hljs-keyword">import</span> okio.BufferedSource;
<span class="hljs-keyword">import</span> okio.ByteString;
<span class="hljs-keyword">import</span> okio.Okio;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test1_png</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException </span>{
        InputStream in = Test1_png.class.getResourceAsStream(<span class="hljs-string">&quot;/com/demo/1.png&quot;</span>);
        decodePng(in);
    }

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> ByteString PNG_HEADER = ByteString
            .decodeHex(<span class="hljs-string">&quot;89504e470d0a1a0a&quot;</span>);

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">decodePng</span><span class="hljs-params">(InputStream in)</span> <span class="hljs-keyword">throws</span> IOException </span>{
        BufferedSource pngSource = Okio.buffer(Okio.source(in));

        ByteString header = pngSource.readByteString(PNG_HEADER.size());
        <span class="hljs-keyword">if</span> (!header.equals(PNG_HEADER)) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(<span class="hljs-string">&quot;Not a PNG.&quot;</span>);
        }

        <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {
            Buffer chunk = <span class="hljs-keyword">new</span> Buffer();

            <span class="hljs-comment">// Each chunk is a length, type, data, and CRC offset.</span>
            <span class="hljs-keyword">int</span> length = pngSource.readInt();
            String type = pngSource.readUtf8(<span class="hljs-number">4</span>);
            pngSource.readFully(chunk, length);
            <span class="hljs-keyword">int</span> crc = pngSource.readInt();

            decodeChunk(type, chunk);
            <span class="hljs-keyword">if</span> (type.equals(<span class="hljs-string">&quot;IEND&quot;</span>))
                <span class="hljs-keyword">break</span>;
        }

        pngSource.close();
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">decodeChunk</span><span class="hljs-params">(String type, Buffer chunk)</span> </span>{
        <span class="hljs-keyword">if</span> (type.equals(<span class="hljs-string">&quot;IHDR&quot;</span>)) {
            <span class="hljs-keyword">int</span> width = chunk.readInt();
            <span class="hljs-keyword">int</span> height = chunk.readInt();
            System.out.printf(<span class="hljs-string">&quot;%08x: %s %d x %d%n&quot;</span>, chunk.size(), type, width,
                    height);
        } <span class="hljs-keyword">else</span> {
            System.out.printf(<span class="hljs-string">&quot;%08x: %s%n&quot;</span>, chunk.size(), type);
        }
    }

}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="collections.html" class="navigation navigation-prev " aria-label="Previous page: 集合">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="advance.html" class="navigation navigation-next " aria-label="Next page: 泛型，注解和反射">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"nio","level":"1.7","depth":1,"next":{"title":"泛型，注解和反射","level":"1.8","depth":1,"path":"advance.md","ref":"advance.md","articles":[]},"previous":{"title":"集合","level":"1.6","depth":1,"path":"collections.md","ref":"collections.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"nio.md","mtime":"2017-04-09T06:01:33.150Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-04-09T09:50:22.406Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

